我和MVC1程序员,MVC2的新手。
在编辑方案中,数据不会持久存储到数据库中。创建工作正常。
控制器:
//
// POST: /Attendee/Edit/5
[Authorize(Roles = "Admin")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Attendee attendee)
{
if (ModelState.IsValid)
{
UpdateModel(attendee, "Attendee");
repository.Save();
return RedirectToAction("Details", attendee);
}
else
{
return View(attendee);
}
}
型号:
[MetadataType(typeof(Attendee_Validation))]
public partial class Attendee
{
}
public class Attendee_Validation
{
[HiddenInput(DisplayValue = false)]
public int attendee_id { get; set; }
[HiddenInput(DisplayValue = false)]
public int attendee_pin { get; set; }
[Required(ErrorMessage = "* required")]
[StringLength(50, ErrorMessage = "* Must be under 50 characters")]
public string attendee_fname { get; set; }
[StringLength(50, ErrorMessage = "* Must be under 50 characters")]
public string attendee_mname { get; set; }
}
我尝试在Class声明上方添加[Bind(Exclude =“attendee_id”)],但随后将attendee_id属性的值设置为“0”。
查看(强类型):
<% using (Html.BeginForm()) {%>
...
<%=Html.Hidden("attendee_id", Model.attendee_id) %>
...
<%=Html.SubmitButton("btnSubmit", "Save") %>
<% } %>
基本上,repository.Save();功能似乎无能为力。我想它与主键约束违规有关。但我没有从SQL Server收到任何错误。应用程序似乎运行正常,但数据永远不会持久保存到数据库。
答案 0 :(得分:0)
知道了!这是解决方案:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection form)
{
Attendee attendee = repository.GetAttendee(id);
try
{
UpdateModel(attendee, form);
repository.Save();
return RedirectToAction("Details", attendee);
}
catch
{
return View(attendee);
}
}
UpdateModel()ValueProvider是问题所在。