0
假设ModelState错误发生在名为AddComment()的操作中。 AddComment没有它自己的视图,所以我们不必返回View(),而是返回View(“Blog”)。我们不能使用RedirectToAction(“Blog”),因为我们放弃了我们的ModelState错误。问题是如果博客视图绑定到博客模型?!?假设我们有一个Index()操作,它的工作是检索Blog的数据并返回View(“Blog”,BlogModel)。我们必须将IndexModel的内容复制到我们的AddComment()操作中。否则,在分析Blog视图时,从AddComment返回View(“Blog”)会给我们一个空的异常。这是维持使用相同视图的操作之间的ModelState错误的唯一方法吗?我刚开始学习MVC,我仍然在学习正确的布局代码的方式,所以请赐教。多个操作,相同视图和验证错误
[HttpGet]
public ActionResult Index()
{
BlogEntry RecentBlogEntry;
//get the most recent blog entry
RecentBlogEntry = m_BlogEntryDataService.GetRecentBlogEntry();
return View(RecentBlogEntry);
}
[HttpPost]
public ActionResult AddComment(BlogComment NewComment)
{
if (ModelState.IsValid)
m_CommentDataService.AddComment(NewComment);
//get the most recent blog entry - AGAIN
return View("Index", m_BlogEntryDataService.GetRecentBlogEntry());
}
但是,如果我有一个更多的博客页面,我会结束很多重复索引操作代码的操作。如果我必须在Index()中进行更改,我将被迫在所有其他操作中更改代码。然后,另一种方法是返回viewstate的日子,在这里我的模型将从请求到请求持久化。我想我只需要确保将我所有的逻辑都保存在我的控制器中! – enamrik 2011-02-03 07:59:51