RAZOR - 如何使用Url.Action将数据传递给控制器​​?

时间:2015-02-10 16:41:34

标签: razor url.action

我有一个显示表和DropDownList的页面 - 它们不包含在表单中。 当用户在DropDownList中选择一个值时 - JavaSript将所选值放在名为selectedCategory的文本框中,该文本框位于以下格式中:

@Html.TextBox("selectedCategory", null, new { @id = "selectedCategory", @style = "width: 100px;" })

我正在使用分页,所以我有:

@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, SortO = ViewBag.SortO, 
      FilterV = ViewBag.FilterV, 
      category = selectedCategory  }))

在这种情况下,我收到错误: 当前上下文中不存在名称“selectedCategory”。 当我在页面源上运行没有“category = selectedCategory”时,我看到该字段存在:

<input id="selectedCategory" name="selectedCategory" style="width: 100px;" type="text" value="" />

所以我的问题是如何添加“category = value of selectedCategory field”?

谢谢,

ZB

1 个答案:

答案 0 :(得分:0)

我再次分析了我的问题,我得出结论,我需要以不同的方式解决我的问题 - 使用Session变量或ViewBag。

原因如下: 代码:

@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, SortO = ViewBag.SortO, 
      FilterV = ViewBag.FilterV, 
      category = xxx  }))

转换为HTML:

<li><a href="/?page=2&SortO=My%20Sort&FilterV=My%20Filter&category=xxx">2</a></li>
  <li><a href="/?page=3&SortO=My%20Sort&FilterV=My%20Filter&category=xxx">3</a></li>
  <li><a href="/?page=4&SortO=My%20Sort&FilterV=My%20Filter&category=xxx">4</a></li> 

用户从DropDownList中选择一个选项后,JavaScript函数将需要更新&#34; category = xxx&#34;与所选选项的价值 - 我认为可以做到,但它看起来很复杂。