2011-12-17 51 views
5

我在做什麼錯我的MVC代碼在這裏?索引視圖包含一個提交給自己的表單,我希望控制器處理提交的表單,然後返回到視圖。RedirectToAction不按預期刷新頁面

實際發生的事情是窗體處理正確,但返回的視圖好像什麼也沒有發生(例如,仍然顯示已刪除的ID)。如果我手動刷新頁面,它會再次正確顯示。我不認爲它是與緩存相關的,因爲重定向到來自不同控制器的相同視圖可以正常工作。我該如何解決它?

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

因此,POST更新由GetComments()檢索到的數據?在POST後,GetComments()應該返回不同的數據? – danludwig 2011-12-17 00:29:04

回答

4

安裝Firefox的Fiddler或Firebug並觀察流量,看它是否真的從瀏覽器(緩存頁面)返回新的響應或HTTP 304。如果一切都檢出,那麼你的數據庫持久性和/或查詢就有問題。

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只有調用的SaveChanges /上提交的請求結束的ORM。 //是否有可能會從集合中刪除項目,但是直到返回GET Index()後才持久保存到db中?

更新

改爲返回RedirectToAction("Index")的,你嘗試過RedirectToAction(Index())

0

嘗試輸入控制器名稱。這幫助了我。例如:

return RedirectToAction("Index","Home");