MVC3 DropDownList - 修改在SelectedIndexChanged上以表格形式显示的数据?

时间:2012-10-31 19:36:46

标签: c# jquery asp.net-mvc asp.net-mvc-3

我的模型中有两个DropDownList元素。

[DisplayName("Site")]
public List<KeyValuePair<int, string>> Site { get; set; }
public int SelectedSiteID { get; set; }
<%= Html.DropDownListFor(model => model.SelectedSiteID, new SelectList(Model.Site, "Key", "Value"))%>

[DisplayName("Data Center")]
public List<KeyValuePair<int, string>> DataCenter { get; set; }
public int SelectedDataCenterID { get; set; }
<%= Html.DropDownListFor(model => model.SelectedDataCenterID, new SelectList(Model.DataCenter, "Key", "Value"))%>

当用户对第一个选择进行更改时,我试图在我的第二个DropDownList中修改ListItem的集合。

我认为这可以通过以下方式实现,但我想确认这是一个很好的做法:

  • 使用jQuery,绑定到第一个DropDownList的更改事件。
  • 响应更改事件时 - 使用新选择的值向服务器发送AJAX请求。
  • 服务器将使用JSON响应,表示第二个DropDownList的ListItem对象集合。
  • 使用jQuery,我修改了第二个DropDownList的选择,使其包含返回的JSON元素。

这一切似乎有点......非MVC3y,如果你愿意的话。这是实现我要求的标准方式,还是有更明确的方式?

2 个答案:

答案 0 :(得分:0)

我认为在这种情况下最好使用Jquery。

最好的办法是将firstDropdown框绑定到Jquery中的OnChange事件,然后使用$ .ajax从Controller中的JSONresult加载数据。加载数据后,将数据加载到secondDropdown中。

如果您需要一些代码帮助,请告诉我。

答案 1 :(得分:0)

我使用jQuery Cascade Select插件为我做这个。它很容易插入,不需要你重新发明轮子。 http://code.google.com/p/jquery-cascade/

相关问题