DFord
DFord

Reputation: 2358

Value from hidden field is not being passed to the EF context model

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" />

Upvotes: 0

Views: 2018

Answers (2)

Jan
Jan

Reputation: 16042

When using @Html.HiddenFor the value for the input is read from your model object. You try to overwrite it by specifying a value for the Value attribute. 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:

return View(new YourModel { LoanStatus = "Not Complete" });

View:

@Html.HiddenFor(m => m.LoanStatus)

Upvotes: 3

Corey Adler
Corey Adler

Reputation: 16137

Instead of using the Html helper here, why not try just making the line manually as follows:

<input type="hidden" name="LoanStatus" value="@ViewBag.LoanStatus"/>

I still don't know why each of your lines has the value set in 2 places. Could you post more code?

EDIT: I think that the lowercase value is being passed back into your controller. Try using @value (lowercase v) instead and see if that fixes it.

Upvotes: 0

Related Questions