Reputation: 1467
I have a Model that is being used in a view which can edit the model. A foreign key in the model is set before displaying the view, and is not touched in the view. The View shows a drop down for that field if it is zero, or skips the drop down if it is not zero.
@if (Model.RepairOrderId == 0)
{
<div class="editor-label">
@Html.LabelFor(model => model.RepairOrderId)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.RepairOrderId, Model.Orders)
@Html.ValidationMessageFor(model => model.RepairOrderId)
</div>
}
else
{
}
When the HTTP POST controller method is called the model contains all the fields that were edited, but the foreign key property that wasn't touched is now empty. What should I put in the else
block to keep the non-zero RepairOrderId?
Upvotes: 0
Views: 147
Reputation: 4120
@Html.HiddenFor(model => model.RepairOrderId)
This generates a hidden input in your html, this way the modelbinder will pick up the value and set it in your viewmodel.
Upvotes: 2