I have an issue cropping up in a form I'm trying to post. In the scenario where the form doesn't validate, I'm taking the standard route of calling ModelState.AddModelError()
then returning a View result.
The thing is, the HTML.* helpers are supposed to pick up the posted value when rendering and I'm noticing that my text fields ONLY do so if I include them in the parameter list of the postback action, which shouldn't be required seeing as some forms have way too many fields to want to list them all as parameters.
My action code is roughly:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditDataDefinition(long? id, string name)
{
var dataDefinition = ...
// do some validation stuff
if (!ModelState.IsValid)
{
// manually set checkbox fields via ViewData seeing as this STILL doesn't work in MC 1.0 :P
// ...
return View(dataDefinition);
}
}
Now, dataDefinition (which is a LINQ to SQL entity) has a field MinVolume, is handled in the view by this line:
Minimum: <%= Html.TextBox("MinVolume", null, new { size = 5 })%>
Yet when the view is rendered after a failed ModelState validation, the value typed into it on the original page we posted is not preserved UNLESS I include it as a parameter in the postback method. Literally, I can "solve the problem" by doing this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditDataDefinition(long? id, string name, string minVolume)
For some reason that will force the field value to be preserved. This seems stupid to me because my form has way more values than just that and I shouldn't have to add a parameter for just that field.
Any ideas?