2013-08-07 29 views
0

我有一個下拉列表和分頁系統的視圖。當我點擊其中一個分頁鏈接時,我需要保留下拉列表的選定索引值。在後期保留下拉列表值ASP.NET MVC

這是下拉列表:

@Html.DropDownList("typeId", types, "-- Filter by type --", new { style= "width: 110px;", onchange = "location.href='" + Url.Action("List", new { month = selectedMonth, year = selectedYear }) + "?typeId=' + $(this).val()" }) 

而且分頁系統:

<div style="margin-top: 15px"> 

    <span> 
     @Html.ActionLink("First", "List", new { month = months[0].Item1, year = months[0].Item3 }) 
    </span> 
    <span style="margin-right: 15px"> 
     @Html.ActionLink("Previous", "List", new { month = previousMonth, year = previousMonthYear }) 
    </span> 
    @foreach (var entry in months) 
    { 
     if (selectedMonth == entry.Item1 && selectedYear == entry.Item3) 
     { 
     <span> 
      @Html.ActionLink(entry.Item2, "List", new { month = entry.Item1, year = entry.Item3 }, new { @class = "current" }) 
     </span> 
     } 
     else 
     { 
     <span> 
      @Html.ActionLink(entry.Item2, "List", new { month = entry.Item1, year = entry.Item3 }) 
     </span> 
     } 
    } 
    <span style="margin-left: 15px"> 
     @Html.ActionLink("Next", "List", new { month = nextMonth, year = nextMonthYear }) 
    </span> 
    <span> 
     @Html.ActionLink("Last", "List", new { month = months[11].Item1, year = months[11].Item3 }) 
    </span> 
</div> 

任何想法讚賞。

PD:我想將所選值作爲分頁鏈接點擊的路徑值傳遞。

我試着這樣做:

@Html.ActionLink("Last", "List", new { month = months[11].Item1, year = months[11].Item3, typeId = "$('#typeId option:selected')" }) 

但我從客戶端(:)錯誤中檢測到有潛在危險的Request的值。我怎樣才能防止這個錯誤?

回答

0

如果會有回發下拉將會丟失它的值,因爲它是MVC我們不能使用ViewState。所以,我的建議是要麼:

  1. 拿該下拉出來表單標籤的
  2. 設值控制器去,然後返回到視圖狀態
  3. 做的意見

不知道部分呈現如果它將有助於您目前的結構。

0

當您使用下拉菜單切換或在查詢字符串和頁面加載中設置頁碼時,可能會使用AJAX加載下一頁,從查詢字符串中獲取頁碼並設置所選值在下降。

0

找到一個解決方法(與幫助的形式我的同事):一個老式的HTML鏈接,在這裏我設置這樣的網址:

<a href="@(Url.Action("List", "Notifications", new { month = months[0].Item1, year = months[0].Item3 }) + (typeId.HasValue ? "/" + typeId.Value : ""))">First</a>