Note that there are some explanatory texts on larger screens.

plurals
  1. POHtml.HiddenFor sporadically not getting set w/ model information
    primarykey
    data
    text
    <p>We're having an issue with Html.HiddenFor in MVC3 <em>occasionally</em> not getting bound properly. We can't reproduce it at all, but we're seeing nullrefs come through in our logging and it's driving us absolutely nuts.</p> <p>We have the following model and controller structure:</p> <pre><code>public class DummyController { [HttpGet] public ActionResult ReturnAPage(int NumericID) { //NumericID should never be 0 or negative, but let's check to make sure if (NumericID &lt; 1) { return RedirectToAction("TracyJordanStabbingRobot"); } return View("DummyView", new DummyViewModel(NumericID)); } [HttpPost] public ActionResult TakePageSubmission(DummyViewModel model) { //AnObject relies on having a non-zero ID ComplexObject AnObject = new ComplexObject(model.NumericID); AnObject.UseMe(); } } public class DummyViewModel { public DummyViewModel() {} public DummyViewModel(int ID) { NumericID = ID; } public int NumericID { get; set; } } </code></pre> <p>... and the following view structure:</p> <p>DummyView.cshtml</p> <pre><code>@model DummyViewModel &lt;html&gt; &lt;head&gt;&lt;/head&gt; &lt;body&gt; &lt;p&gt;THIS IS A VIEW!&lt;/p&gt; &lt;form id="DummyViewForm" action="/RouteTo/TakePageSubmission" method="post"&gt; @Html.Partial("_PartialDummyView", Model) &lt;input type="submit" value="Submit This!" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>_PartialDummyView.cshtml</p> <pre><code>@model DummyViewModel &lt;p&gt;Heard you like views...&lt;/p&gt; @Html.HiddenFor(model =&gt; model.NumericID) </code></pre> <p>Considering we're checking for less-than-zero values in the initial controller action, it stands to reason that <code>@Html.HiddenFor(model =&gt; model.NumericID)</code> should never have a less-than-zero value. </p> <p>That being said, when we get to using <code>AnObject</code> in the <code>TakePageSubmission</code> action, we're getting null reference errors. </p> <p>When we dug into logging the <code>model.NumericID</code> value, we're seeing it come through as zero, which <em>shouldn't be possible</em> considering the DummyView can only be accessed with a non-zero value. </p> <p>We're a little stumped and since we can't reliably reproduce the issue, we have <em>no</em> idea what could possibly be causing it. Has anyone run into something like this before?</p> <p>Edit: We are doing ModelState validation on the form post, but we're not checking to see if the NumericID coming through is 0. When we did check for that, the model came through as invalid, which just proves that the HiddenFor is getting set improperly. Furthermore, the route to the page actually <em>includes</em> the NumericID, so for example, we've seen this happen on:</p> <p><a href="http://our.site.com/RouteToReturnAPage/1736/" rel="nofollow">http://our.site.com/RouteToReturnAPage/1736/</a></p> <p>...where the parameter for the action is clearly set, the model is constructed correctly, but for some unknown reason the HiddenFor NumericID value is 0. It's really baffling.</p>
    singulars
    1. This table or related slice is empty.
    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