我有一个家庭控制器
public class HomeController : Controller
{
public ActionResult Index()
{
var m = new ModelClass
{
Prop1 = 1,
Prop2 = "property 2"
};
return View(m);
}
public ActionResult SubAction()
{
ModelState.AddModelError("key", "error message value");
return RedirectToAction("Index");
}
}
模型:
public class ModelClass
{
public int Prop1 { get; set; }
public string Prop2 { get; set; }
}
并查看:
@model MvcApplication9.Models.ModelClass
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.TextBoxFor(m => m.Prop1)
@Html.TextBoxFor(m => m.Prop2)
@Html.ValidationMessage("key")
<br/>
@Html.ActionLink("action", "SubAction", "Home")
当我单击action
actionlink 时,我希望看到error message value
,但是当我从重定向SubAction
到Index
action时,ModelState
错误会丢失。如何保存这些模型错误,设置在 中SubAction
,并在视图中显示它们,由Index
操作返回?