2013-08-07 30 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>