我有一个控制器2种动作方法,MVC验证和错误处理周期
指数:
public ActionResult Index(string url)
{
// take the url as a param and do long tasks here
ViewBag.PageTitle = "title";
ViewBag.Images = "images";
// and some more view bags
return View();
}
该索引视图包含其张贴到在同一个控制器的另一种方法的一种形式。
public ActionResult PostMessage(string msg, string imgName)
{
// save data in the db
// but on error I want to navigate back to the Index view but without losing data the user fielded before submit the form.
// Also need to pass an error message to this index view to show
}
如何回返回索引视图是否有出错的PostMessage的方法,也不要清除表单域,再加上示出了指定的PostMessage的方法的错误消息。
我需要知道做这种情况的最佳做法。
和我从索引方法发送到索引视图显示,在这种情况下,它们为空的ViewBag(s)怎么样。 –
@Amr ElGarhy,如果你重新显示相同的视图,你将不得不在'PostMessage'中再次设置它们。顺便说一下,你不应该使用任何ViewBag。我会建议你使用视图模型。 –