1

我在 MVC 中的 POST 有点问题。

我有我的模型

    [ScaffoldColumn(false)]
    public int Id { get; set; }

    [ScaffoldColumn(false)]
    public Guid UId { get; set; }

    [Required(ErrorMessage = "Indtast dato")]
    [DataType(DataType.Time)]
    public DateTime Date { get; set; }

    [Required(ErrorMessage = "Indtast overskrift")]
    public string Headline { get; set; }

    [Required(ErrorMessage = "Indtast text")]
    [AllowHtml]
    public string Text { get; set; }

我可以从我的模型中插入:

@using (Html.BeginForm(ViewData["methode"].ToString(), "News", Model, FormMethod.Post))
{
    @Html.HiddenFor(n => n.Id)
    <table>
        <tr>
            <td style="width:65px">
              @Html.LabelFor(n => n.Date, "Dato")
            </td>
            <td style="width:100%">
              @Html.Telerik().DatePickerFor(n => n.Date).Value(@Model.Date)
            </td>
        </tr>
        <tr>
            <td>
              @Html.LabelFor(n => n.Headline, "Overskrift")
            </td>
            <td>
              @Html.TextBoxFor(n => n.Headline, new { @class = "form_smallbox" })
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td colspan="2">
            @(Html.Telerik().EditorFor(e => e.Text)
                            .Encode(false)
                            .Tools(tools => tools.Clear()
                                 .Bold()
                                 .Italic()
                                 .Underline()
                                 .Separator()
                                 .JustifyLeft()
                                 .JustifyCenter()
                                 .JustifyRight()
                                 .Separator()
                                 .FontSize()
                                 .FontColor()
                                 .FormatBlock()
                                 .Separator()
                                 .InsertUnorderedList()
                                 .InsertOrderedList()
                                 .CreateLink()
                            )
                            .Localizable("da-DK")
                            .HtmlAttributes(new { style = "height:350px;" })
            )
            </td>
        </tr>
    </table>
    @Html.ValidationSummary()

    <input type="image" src="../../Content/Image/btn_save.png" title="Gem nyhed" />
}

[HttpPost]
public ActionResult InsertNews(DisplayNews news)
{
    if (ModelState.IsValid)
    {
        news.Text = "<html><head></head><body>" + news.Text + "</body></html>";
        News newNews = new News
        {
            UId = Guid.NewGuid(),
            Date = news.Date,
            Headline = news.Headline,
            Text = MaterialeLagretLibrary.Helpers.StrToByteArray(news.Text)
        };
        MaterialeLagretLibrary.DBHandler.NewsDBHandler.InsertNews(newNews);
    }
    return RedirectToAction("News");
}

这没有问题。但随后我会进行更新,我使用 ActionNameUpdateNews而不是InsertNews.

控制器看起来几乎相同:

[HttpPost]
public ActionResult UpdateNews(Display news)
{
    if (ModelState.IsValid)
    {
        news.Text = "<html><head></head><body>" + news.Text + "</body></html>";
        MaterialeLagretLibrary.DBHandler.NewsDBHandler.UpdateNews(news);
    }
    return RedirectToAction("News");
}

现在的问题是我收到一个错误:

"此请求的查询字符串长度超过了配置的 maxQueryStringLength 值。 "

它使 QueryString 好像它是 GET 而不是 POST。

我究竟做错了什么?

渲染后的表单头:

<form method="post" action="/News/UpdateNews/0?UId=00000000-0000-0000-0000-000000000000&Date=06%2F13%2F2012%2000%3A00%3A00&Headline=Nyhedsbrev&Text=%3Chtml%3E%3Chead%3E%3C%2Fhead%3E%3Cbody%3E%3Cp%3E%3Cspan%20style%3D%22color%3A%23ff7f27%3B%22%3EKATALOG%20NR%201037%3C%2Fspan%3E%3Cbr%20%2F%3E%3Cspan%20style%3D%22color%3A%23ff7f27%3B%22%3EHJ%C3%86LP%20%E2%80%93%20EN%20ELEV%20MED%20H%C3%98RETAB%20I%20MIN%20KLASSE!%3C%2Fspan%3E%3Cbr%20%2F%3E%3Cspan%20style%3D%22color%3A%23ff7f27%3B%22%3E%E2%80%9DHj%C3%A6lp%20-%20en%20elev%20med%20h%C3%B8...E-p%C3%A6dagoger%20og%20taleh%C3%B8rep%C3%A6dagoger%2C%20som%20i%20deres%20hverdag%20arbejder%20med%20b%C3%B8rn%20med%20h%C3%B8retab.%3C%2Fspan%3E%3Cbr%20%2F%3E%3Cspan%20style%3D%22color%3A%23ff7f27%3B%22%3EFor%20l%C3%A6rere%20og%20p%C3%A6dagoger%20i%20folkeskolen%2C%20som%20f%C3%A5r%20et%20inkluderet%20barn%20med%20h%C3%B8retab%2C%20vil%20denne%20bog%20v%C3%A6re%26nbsp%3B%3C%2Fspan%3E%3Cspan%20style%3D%22color%3A%23ff7f27%3B%22%3Een%20rigtig%20god%20guide.%3C%2Fspan%3E%3C%2Fp%3E%3C%2Fbody%3E%3C%2Fhtml%3E">

我看到我需要从操作中删除查询字符串。

4

1 回答 1

1

如果它是 GET,它永远不会到达标记为 POST 的操作,因此无需发布。

您的模型应该是路线值吗?我会检查可用的 Html.BeginForm 重载,因为 FormMethod.Post 的参数值显然会指向其他参数。


更新:

在检查您确实想在模型中添加路由值之后,我想我知道您的问题是什么:

您实际上是将表单作为 POST 提交,但是由于路由值(您的模型),查询字符串无论如何都太长了。

加载您的页面并获取源代码,您会发现与此类似的内容:

<form action="/yourpage?too-many-parameters-here-which-make-your-query-string-too-long" method="post">

表单实际上是作为 POST 提交的,但 URL 太长。我不知道您在这里的目的,但您可以将模型数据作为隐藏字段包含在表单中,或者减少您要发送的值的数量。

于 2012-06-14T14:53:36.183 回答