I am working on a sample MVC Nerdinner tutorial and use it in the AdventureWorks database. I created the "Edit" action in the CategoryController to edit the product category in AdventureWorks. The only field to be updated in this table is Name (other fields - ID, RowGUID and UpdateDate are automatically generated). Thus, my right View form has only 1 field for the name (product category). My Save action for editing is shown below: -
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection){
ProductCategory p = awRepository.GetProductCategory(id);
try
{
p.Name = Request.Form["Name"];
awRepository.Save();
return RedirectToAction("Details", new { id = p.ProductCategoryID });
}
catch
{
foreach (var err in p.GetRuleViolations())
{
ModelState.AddModelError(err.PropertyName, err.ErrorMessage);
}
return View(p);
}
}
, , , , , ( , ). ( GetRuleViolations, "" ), NullReferenceException ( , ) (Category/Edit.aspx), Edit View ( )
<%= Html.TextBox("Name") %>
ProductCategory UpdateModel (p) Request.Form, ; , , .
: UpdateModel , Request.Form? Nerdinner, , , . , , .