MVC3 POST做GET

时间:2012-06-14 13:21:34

标签: jquery asp.net-mvc-3 post get

我在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");
}

没有问题。但是我会做一个更新,我使用ActionName UpdateNews而不是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">

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

1 个答案:

答案 0 :(得分: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太长。我不知道您的目的,但您可以将模型数据包含在表单中的隐藏字段中,或者减少要发送的值。