asp.net-mvc-3 – 选择更改事件 – Html.DropDownListFor

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – 选择更改事件 – Html.DropDownListFor前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个下拉列表。从第一个选择的值加载另一个。当我在控制器中有帮助方法时,该怎么做?
@using (Html.BeginForm())
{
<div>
    <table width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td><b>Select a District:</b></td>
            <td>@Html.DropDownListFor(m => m.DistrictId,ViewData["DMManagers"] as IEnumerable<SelectListItem>,"Select One")</td>
        </tr>
        <tr>
            <td><b>Select a TM:</b></td>
            <td>@Html.DropDownListFor(m => m.TMId,ViewData["TMManagers"] as IEnumerable<SelectListItem>,"Select One")</td>
        </tr>
    </table>
</div>
}

private void LoadDistrictManagers()
{
    var _DMS = (from c in SessionHandler.CurrentContext.ChannelGroups
                join cgt in SessionHandler.CurrentContext.ChannelGroupTypes on c.ChannelGroupTypeId equals cgt.ChannelGroupTypeId
                where cgt.Name == "District Manager"
                select new { c.ChannelGroupId,c.Name }).OrderBy(m => m.Name);
    ViewData["DMManagers"] = new SelectList(_DMS,"ChannelGroupId","Name");
}

private void LoadTerritoryManagers(int districtId)
{
    var _TMS = (from c in SessionHandler.CurrentContext.ChannelGroups
                join cgt in SessionHandler.CurrentContext.ChannelGroupTypes on c.ChannelGroupTypeId equals cgt.ChannelGroupTypeId
                where cgt.Name == "Territory" && c.ParentChannelGroupId == districtId
                select new { c.ChannelGroupId,c.Name }).OrderBy(m => m.Name);
    ViewData["TMManagers"] = new SelectList(_TMS,"Name");
}

public ActionResult SummaryReport()
{
    DistrictManagerModel model = new DistrictManagerModel();
    LoadDistrictManagers();
    return View("AreaManager",model);
}

解决方法

使用HTTPAttributes字段给两个下拉列表的唯一ID:
@Html.DropDownListFor(m => m.DistrictId,"Select One",new {@id="ddlDMManagers"})

应将第二个下拉列表初始化为空列表:

@Html.DropDownListFor(m => m.TMId,Enumerable.Empty<SelectListItem>(),new {@id="ddlTMManagers"})

如果您不介意使用jQuery ajax在第一个下拉列表中触发“更改”事件时更新第二个下拉列表:

$(function() {
    $('select#ddlDMManagers').change(function() {
        var districtId = $(this).val();


        $.ajax({
            url: 'LoadTerritoryManagers',type: 'POST',data: JSON.stringify({ districtId: districtId }),dataType: 'json',contentType: 'application/json',success: function (data) {
                $.each(data,function (key,TMManagers) {
                    $('select#ddlTMManagers').append('<option value="0">Select One</option>');
                    // loop through the TM Managers and fill the dropdown
                    $.each(TMManagers,function(index,manager) {
                        $('select#ddlTMManagers').append(
                            '<option value="' + manager.Id + '">'
                            + manager.Name + 
                            '</option>');
                    });
                });
            }
        });
    });
});

将此类添加到您的控制器命名空间中:

public class TMManager
{
    public int Id {get; set;}
    public string Name {get; set;}
}

您将需要更新控制器操作LoadTerritoryManagers(),以响应ajax请求并返回一个{Id,Name}对象的JSON数组。

[HttpPost]
    public ActionResult LoadTerritoryManagers(int districtId)
    {
        var _TMS = (from c in SessionHandler.CurrentContext.ChannelGroups
                join cgt in SessionHandler.CurrentContext.ChannelGroupTypes on c.ChannelGroupTypeId equals cgt.ChannelGroupTypeId
                where cgt.Name == "Territory" && c.ParentChannelGroupId == districtId
                select new TMManager(){ Id = c.ChannelGroupId,Name = c.Name }).OrderBy(m => m.Name);

        if (_TMS == null)
            return Json(null);

        List<TMManager> managers = (List<TMManager>)_TMS.ToList();
        return Json(managers);
    }

猜你在找的asp.Net相关文章