Reputation: 373
I have a bit of a problem with POST in MVC.
I have my model
[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; }
I can do a insert from my model:
@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");
}
There is no problem in that. But then I will do a Update, I use the ActionName UpdateNews
instead of InsertNews
.
The controller looks almost the same:
[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");
}
The problem is now that I get an error:
"The length of the query string for this request exceeds the configured maxQueryStringLength value."
And it make a QueryString as if it was a GET instead POST.
What am I doing wrong?
The form header after rendering:
<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">
I see i need to remove the querystring from the action.
Upvotes: 1
Views: 412
Reputation: 8474
If it's a GET, it never reaches the action marked as POST, so no need to post that.
Is your model supposed to be routes values? I would check the Html.BeginForm overloads available because the argument value of FormMethod.Post is clearly going to some other parameter.
After checking that you really want to add the route values with your model, I think I know what your problem is:
You're actually submitting the form as POST, but the query string is just too long anyway because of the route values (your model).
Load your page and get the source and you'll find something similar to this:
<form action="/yourpage?too-many-parameters-here-which-make-your-query-string-too-long" method="post">
The form is actually submitted as POST but the URL is too long. I don't know your purpose here but you may include the model data as hidden fields in the form or reduce the amount of values you want to send.
Upvotes: 1