2013-01-08 18 views
0

當我捕捉到HttpPost時,我正在重定向到另一個ResultAction。它保留我的int值,但不是我的列表值。似乎無法弄清楚爲什麼。如果我得到頁碼爲2的帖子,searchAction = 3並且帶有25個項目的clearanceResults(一個列表)。它回來了我期望的帖子,但是當我到達Details ActionResult時,它只保留pageNumber和searchAction,而不是clearanceResults的列表。奇怪的事情,該列表不爲空,它只是具有計數0使用RedirectToAction時,它不保留我的模型

型號:

public class ClearanceListViewModel 
{ 
    public ClearanceListViewModel() 
    { 
     this.pageNumber = 1; 
     this.searchAction = 1; 
     this.lastPage = false; 
    } 

    public ClearanceListViewModel(int pageNumber, int searchAction) 
    { 
     this.pageNumber = pageNumber; 
     this.searchAction = searchAction;    
    } 

    public int pageNumber { get; set; } 
    public int searchAction { get; set; } 
    public List<ClearanceViewModel> clearanceResults { get; set; } 
    public bool lastPage { get; set; } 
} 

郵政控制器:控制器

[HttpPost] 
    public ActionResult Details(ClearanceListViewModel model, FormCollection collection) 
    { 
     ClearanceListViewModel cModel = new ClearanceListViewModel(); 
     cModel = model; 
     cModel.clearanceResults = model.clearanceResults; 
     // do something 
     return RedirectToAction("Details", cModel); 
    } 

操作結果:

public ActionResult Details(ClearanceListViewModel model) 
    { 

     DataTable dt = new DataTable(); 
     List<ClearanceViewModel> clearanceList = new List<ClearanceViewModel>(); 

     //save any changes 
     if (model.clearanceResults != null) 
     { 
      ClearanceSave(model); 
      model.clearanceResults = null; 
     } 

     string inQuery = "select sku, qty from products"; 

     // call the query 
     dt = AS400DAL.Instance.ExecuteQueryAsDataTable(inQuery); 

     model = Utility.Utility.ProcessClearanceSkus(dt, model); 
     return View("Index",model); 
    } 

任何輸入,將不勝感激。

謝謝!

回答

2

研究RedirectToAction的過載情況。沒有一個模型允許傳遞。通常你的文章會修改數據庫,然後你會重定向到一個從數據庫重新創建模型的動作。因爲重定向是在客戶端發生的事情,所以重定向的請求完全獨立於發佈重定向的帖子,所以模型不會被持久化。

0

使用會話存儲模型,

你可以這樣做:

Session["mymodel"] = model; 

那麼你重定向後做

ClearanceListViewModel newModel = (ClearanceListViewModel)Session["mymodel"]; 

這將允許你從會話模型成功通過該模型

+1

一切都很好,直到您在網站上發佈,然後輪子脫落。 – spender

+0

@spender是否發佈到網絡農場? – CR41G14

+1

如果沒有,那麼這是沒有問題的,但我確實認爲在web服務器中持久化狀態有一個難聞的氣味。 – spender

相關問題