我正在使用NerdDinner MVC1代码。创建了一个名为DinnerFormViewModel的视图模型:
public class DinnerFormViewModel
{
public Dinner Dinner { get; private set; }
public SelectList Countries { get; private set; }
public DinnerFormViewModel(Dinner dinner)
{
Dinner = dinner;
Countries = new SelectList(PhoneValidator.Countries, dinner.Country);
}
}
我传递给我的编辑视图很好,它在MVC2中使用强类型助手。然而,回过头来:
public ActionResult Edit(int id, FormCollection collection)
{
Dinner dinnerToUpdate = dr.GetDinner(id);
try
{
UpdateModel(dinnerToUpdate, "Dinner"); // using a helper becuase of strongly typed helpers to tell it what to update
// updates the properites of the dinnerToUpdate object using incoming form parameters collection.
// UpdateModel automatically populates ModelState when it encounters errors
// works by when trying to assign BOGUS to a datetime
// dinnerToUpdate.Country = Request.Form["Countries"];
dr.Save(); // dinner validation may fail here too due to hook into LINQ to SQL via Dinner.OnValidate() partial method.
return RedirectToAction("Details", new { id = dinnerToUpdate.DinnerID });
}
这样可行,但我的国家/地区没有更新,因为我只是提示UpdateModel来更新晚餐。
问题:如何保存国家?
感谢。