我有这段代码:
//Edit page
public ActionResult Edit(string id)
{
MyObj myobj = db.MyObj.First(s=>s.id == id);
return View(myobj);
}
//Save the edition
[HttpPost]
public ActionResult Edit(string id, MyObj myobj)
{
if( !ModelState.IsValid ) return View(myobj);
//Continue saving...
}
因此,如果传递的数据错误,模型验证失败并返回带有数据注释的obj,问题是我在url中丢失了参数。
修改网址为http://xxx/controller/Edit/1234,下一个为http://xxx/controller/Edit(不含参数)
答案 0 :(得分:0)
public ActionResult Edit(string id, MyObj myobj)
{
if( !ModelState.IsValid ){
ViewBag.Obj=myobj;
return RedirectToAction("Edit",new{id=id});
}
//Continue saving...
}