I have the following hidden fields inside my View:
@Html.HiddenFor(model => model.cat.Id)
@Html.HiddenFor(model => model.cat.OwnerId)
@Html.HiddenFor(model => model.cat.BirthDate
@Html.HiddenFor(model => model.cat.Weight)
Id is a Key and OwnerId is a ForeignKey. When I try to edit that model, all values are displayed correctly but the problem is when I try to POST those values back. Id and OwnerId are always 0. I have a lot of other values inside my view like dropdown lists, text boxes etc, all those values are posted correctly. The problem is only with Id and OwnerId.
Controller:
[HttpPost]
public ActionResult Edit(EditCatDetailsViewModel model)
{
Debug.WriteLine("Cat Id: " + model.cat.Id); //displays 0
}
I checked source of web site and I see Id and OwnerId have correct values (values aren't 0). My question is why are those values not sent like all other values? Why are they received in controller as 0?
Thank you!
UPDATE:
Cat Edit View:
@model Pro.Web.Models.EditCatDetailsViewModel
@{
ViewBag.Title = "Edit";
}
<h2>Edit Cat Details</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Cat</legend>
@Html.HiddenFor(model => model.cat.Id)
@Html.HiddenFor(model => model.cat.OwnerId)
@Html.HiddenFor(model => model.cat.BirthDate
@Html.HiddenFor(model => model.cat.Weight)
<div class="editor-label">
@Html.LabelFor(model => model.cat.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.cat.Name)
@Html.ValidationMessageFor(model => model.cat.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.cat.Description)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.cat.Description, new { @style="width:400px; height:600px" })
@Html.ValidationMessageFor(model => model.cat.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.cat.NumberOfKittens)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.cat.NumberOfKittens)
@Html.ValidationMessageFor(model => model.cat.NumberOfKittens)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to Details", "Details")
</div>