C# 从控制器返回有效视图
我有一个这样的方法:C# 从控制器返回有效视图,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个这样的方法: [HttpPost] public ActionResult Edit(ViewModel.MyViewModel viewModel) { // Code here to handle the save to the database and in the case // where some validation are not accepted, return View(viewModel) return View
[HttpPost]
public ActionResult Edit(ViewModel.MyViewModel viewModel)
{
// Code here to handle the save to the database and in the case
// where some validation are not accepted, return View(viewModel)
return View("Index", viewModel);
}
当我运行此代码并且我的viewModel有效时,我希望将用户返回到视图,并显示项目列表
我知道在这种情况下我必须使用RedirectToAction(“Index”)而不是最后一行,但我想知道为什么当我运行这段代码并执行最后一行时,MVC会返回编辑页面而不是列表页面
如果我不能指定除控制器处理的视图之外的其他视图,那么出于哪些原因,我会在视图函数的参数列表中指定视图名称
多谢各位
我知道在这种情况下我必须使用RedirectToAction(“索引”)
而不是最后一行,但我想知道为什么当我运行此代码时
执行最后一行时,MVC返回编辑页面,而不是
列表页面
不执行
编辑
视图。搜索一段时间后,即使在浏览器地址栏中看到/somecontroller/edit
,也会将MyViewModel
的实例传递到索引
视图,我最终发现问题在于我将错误的viewmodel发送到了索引页面,因为该页面接收到了viewmodel的强类型列表。在本例中,我在Firefox的HttpFox插件帮助下搜索了简单返回视图(“索引”)和重定向操作(“索引”)之间的区别,最后一个显然是所有方面的最佳选择。首先,它发送一个http状态代码302,这最好符合PRG模式,其次,我不必复制索引操作的逻辑来填充viewmodel列表。