I’m pretty sure this is because the form only POSTS inputs when submitting a form, no?
So when I recieve a POST response, the name I edited is correctly received but the ID is always set to 0.
Any workarounds?
public ActionResult Edit(int id)
{
var productBrand = brandRepo.FindProductBrand(id);
ProductBrandModel model = Mapper.Map<ProductBrand, ProductBrandModel>(productBrand);
return View(model);
}
[HttpPost]
public ActionResult Edit(ProductBrandModel model)
{
if (ModelState.IsValid)
{
var productBrand = brandRepo.FindProductBrand(model.ProductBrandId);
productBrand.Name = model.Name;
brandRepo.SaveChanges();
return RedirectToAction("Index", "ProductBrands");
}
return View(model);
}
/* THIS IS THE VIEW*/
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>ProductBrandModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ProductBrandId)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.ProductBrandId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
I don’t want to let the users edit the ID of the entity, only the name.
Thank you.
What about adding
@Html.HiddenFor(model => model.ProductBrandId)I think that will do what you want, if I understand your question.