I have a few values that I’ve put in some hidden fields of a form using Razor. When I submit the form, it does not pass the validation that Entity Framework created in the context.tt model of a certain field. When I submit, there is an error thrown saying that the field cannot be null. This led me to believe that the value was null in the HTML. When I checked the source this is what i found:
<input Value="1" data-val="true" data-val-number="The field PoolType must be a number." id="PoolType" name="PoolType" type="hidden" value="" />
<input Value="Not Complete" id="LoanStatus" name="LoanStatus" type="hidden" value="" />
<input Value="2012-12-12 09:26.39" data-val="true" data-val-required="The DateStamp field is required." id="DateStamp" name="DateStamp" type="hidden" value="" />
I’m wondering why the input with id LoanStatus has a value set but it is throwing an error saying that it cannot be null. The exception thrown is:
System.Data.ConstraintException This property cannot be set to a null value.
To set the value in the HTML, I set it in the controller using the ViewBag, ViewBag.LoanStatus = "Not Complete";. Then in the cshtml file @Html.HiddenFor(model => model.LoanStatus, new { @Value = ViewBag.LoanStatus })
Is there something I am missing, I am new to MVC and Entity Framework.
EDIT – Additional Code
More hidden elements
@Html.HiddenFor(model => model.ID, new{ @Value = ViewBag.ID })
@Html.HiddenFor(model => model.Reason, new{ @Value = ViewBag.Reason })
@Html.HiddenFor(model => model.PoolType, new{ @Value = ViewBag.PoolType })
@Html.HiddenFor(model => model.LoanStatus, new{ @Value = ViewBag.LoanStatus })
@Html.HiddenFor(model => model.DateStamp, new{ @Value = ViewBag.DateTime })
And the html that is produced
<input Value="55" data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field is required." id="ID" name="ID" type="hidden" value="" />
<input Value="1" data-val="true" data-val-number="The field Reason must be a number." id="Reason" name="Reason" type="hidden" value="" />
<input Value="1" data-val="true" data-val-number="The field PoolType must be a number." id="PoolType" name="PoolType" type="hidden" value="" />
<input Value="Not Complete" id="LoanStatus" name="LoanStatus" type="hidden" value="" />
<input Value="2012-12-12 10:00.46" data-val="true" data-val-required="The DateStamp field is required." id="DateStamp" name="DateStamp" type="hidden" value="" />
EDIT Code without Html helper
Line from .cshtml file
<input type="hidden" name="LoanStatus" id="LoanStatus" data-val="true" value="@ViewBag.LoanStatus" />
Line from html source
<input type="hidden" name="LoanStatus" id="LoanStatus" data-val="true" value="Not Complete" />
When using
@Html.HiddenForthe value for the input is read from your model object. You try to overwrite it by specifying a value for theValueattribute. I guess this won’t work and as you see, you get empty value attributes.To solve your problem, don’t use the viewbag but pass a model object with the values set to your view like this:
Controller:
View: