Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.Net MVC ModelState / Html.TextBox postback issue
    primarykey
    data
    text
    <p>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 <code>ModelState.AddModelError()</code> then returning a View result.</p> <p>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.</p> <p>My action code is roughly:</p> <pre><code>[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); } } </code></pre> <p>Now, dataDefinition (which is a LINQ to SQL entity) has a field <em>MinVolume</em>, is handled in the view by this line:</p> <pre><code>Minimum: &lt;%= Html.TextBox("MinVolume", null, new { size = 5 })%&gt; </code></pre> <p>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:</p> <pre><code>[AcceptVerbs(HttpVerbs.Post)] public ActionResult EditDataDefinition(long? id, string name, string minVolume) </code></pre> <p>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.</p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload