我的MVC代码在这里做错了什么?索引视图包含一个提交给自己的表单,我想要的是控制器处理提交的表单然后返回到视图。
实际发生的是表单处理正确,但返回的视图就好像什么也没发生(例如,仍然显示已删除的ID)。如果我手动刷新页面,它会再次正确显示。我不认为这是相关的broswer缓存,因为从不同的控制器重定向到相同的视图工作正常。我该如何解决?
public ViewResult Index()
{
return View(GetComments());
}
[HttpPost]
public ActionResult Index(int[] AllIds)
{
if (AllIds != null)
{
foreach (int id in AllIds)
{
// do stuff
}
}
return RedirectToAction("Index");
}
编辑:提交表单时,第一个方法的断点未被点击并尝试“单步进入(F11)”return RedirectToAction("Index");
行直接转到最终}
。
答案 0 :(得分:4)
为Firefox安装Fiddler或Firebug并观察流量,看到它确实从浏览器返回新响应或HTTP 304(缓存页面)。如果一切都检查出来,那么你的数据库持久性和/或查询就会出现问题。
答案 1 :(得分:1)
public ViewResult Index()
{ // breakpoint
var comments = GetComments(); // debug and inspect the value of this variable
return View(comments);
}
[HttpPost]
public ActionResult Index(int[] AllIds)
{
if (AllIds != null)
{
foreach (int id in AllIds)
{
// do stuff
}
}
return RedirectToAction("Index"); // breakpoint
}
我知道有些人在MVC中使用IUnitOfWork,它只在请求结束时调用ORM上的SaveChanges / Commit。是否有可能// do stuff从集合中删除项目,但是在返回GET Index()之后才会持久保存到db?
<强>更新强>
您是否尝试RedirectToAction("Index")
?
RedirectToAction(Index())
答案 2 :(得分:0)
尝试输入控制器名称。这对我有所帮助。例如:
image=ap.jpg