2012-04-21 23 views
0

我有兩種形式之一的視圖,用於每頁聲明行。在行動後,我需要我的網址是一樣的,只需添加新的參數。在我現在用這樣的:將值添加到RoutValues並保留ASP.NET MVC中的當前值3

[HttpPost] 
public ActionResult Index(FormCollection collection) { 

    //Calculate Row Count 

    return RedirectToAction("Index", new { RC = RowCount }); 

    } 

這個實現我所有的參數丟失,只是RC = rowcountnumber取代。 我該如何保持參數,只需添加新的RC?什麼是最快的方式來做到這一點?這裏有任何性能問題?

回答

0

檢查這個,我不知道是關於性能問題或最快的,但工作:

RouteValueDictionary rt = new RouteValueDictionary(); 
    foreach(string item in Request.QueryString.AllKeys) 
     rt.Add(item, Request.QueryString.GetValues(item).FirstOrDefault()); 

    rt.Add("RC", RowCount); 
    return RedirectToAction("Index", rt); 
0

可惜我現在不能測試,但我懷疑這會工作(雖然它的變異收集參數)。它會表現良好,因爲它不會複製舊集合中的任何項目;只需添加一個項目。

[HttpPost] 
    public ActionResult Index(FormCollection collection) 
    { 
     //Calculate Row Count 
     collection.Add("RC", RowCount); 
     return RedirectToAction("Index", collection); 
    } 
+0

那不是工作 – Saeid 2012-04-22 11:29:34

0

使窗體GETPOST因爲請求是在服務器上沒有改變任何東西,所以GET動詞是比較合適的,也是GET動詞將參數傳遞的所有表單值作爲查詢字符串,所以你」會得到預期的效果

做的是,在Html.BeginForm第三個參數應該是FormMethod.Get

相關問題