0

我是 mvc 新手,我有 2 个下拉列表,我需要使用 mvc 3 绑定第二个下拉列表。

代码示例

@<table>
    <tr>
        <td>
            @Html.DropDownList("Brands", Model.Brands, "Select Brand", New With {.style = "width:150px;"})
        </td>
    </tr>
    <tr>
        <td>
            @Html.DropDownList("Models", Model.Models, "Select Model", New With {.style = "width:150px;"})
        </td>
    </tr>
    <tr>
        <td>
            @Html.DropDownList("Devices", Model.Devices, "Select Device", New With {.style = "width:150px;"})
        </td>
    </tr>
    <tr>
        <td>
            @Html.DropDownList("Systems", Model.Systems, "Select System", New With {.style = "width:150px;"})
        </td>
    </tr>
</table>

我需要按品牌填充模型并按模型填充设备。任何帮助请..

谢谢

4

1 回答 1

0

鉴于:

@Html.DropDownListFor( x => x.Model, new SelectList( LookupUtils.GetModelsList(Model.Brand), "Value", "Text", Model.Model))

LookupUtils 是一个静态类有:

public static List<SelectListItem> GetModelsList(int brand)
{
    var dataContext = new YourDataContext(  );
    var data = dataContext.GetModelsFn(brand).ToList();

    var result = ( from res in data
                   select new SelectListItem()
                              {
                                  Text = res.modelName,
                                  Value = res.modelId.ToString()
                              } ).ToList();

    return result;
}

对于动态更新一些下拉列表,你应该使用 jQuery-Collapse 插件:http: //github.com/danielstocks/jQuery-Collapse/

于 2012-10-16T13:38:05.547 回答