在选择更改事件 - Html.DropDownListFor [英] on select change event - Html.DropDownListFor

查看:199
本文介绍了在选择更改事件 - Html.DropDownListFor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个DropDownList中。从第一个所选择的值加载其他。我该怎么做,当我有一个控制器的辅助方法?

  @using(Html.BeginForm())
{
< D​​IV>
    <表格的宽度=100%的cellpadding =0CELLSPACING =0>
        &所述; TR>
            < TD>< B>选择区:LT; / B>< / TD>
            < TD> @ Html.DropDownListFor(M = GT; m.DistrictId,计算机[DMManagers]为IEnumerable< SelectListItem>中选择一项)< / TD>
        < / TR>
        &所述; TR>
            < TD>< B>选择TM:其中; / B>< / TD>
            < TD> @ Html.DropDownListFor(M = GT; m.TMId,计算机[TMManagers]为IEnumerable< SelectListItem>中选择一项)< / TD>
        < / TR>
    < /表>
< / DIV>
}私人无效LoadDistrictManagers()
{
    VAR _DMS =(从SessionHandler.CurrentContext.ChannelGroupsÇ
                加入CGT在SessionHandler.CurrentContext.ChannelGroupTypes上c.ChannelGroupTypeId等于cgt.ChannelGroupTypeId
                其中,cgt.Name ==地区经理
                选择新的{c.ChannelGroupId,c.Name})排序依据(M =方式> m.Name);
    计算机[DMManagers] =新的SelectList(_DMS,ChannelGroupId,姓名);
}私人无效LoadTerritoryManagers(INT districtId)
{
    VAR _TMS =(从SessionHandler.CurrentContext.ChannelGroupsÇ
                加入CGT在SessionHandler.CurrentContext.ChannelGroupTypes上c.ChannelGroupTypeId等于cgt.ChannelGroupTypeId
                其中,cgt.Name ==领地&放大器;&安培; c.ParentChannelGroupId == districtId
                选择新的{c.ChannelGroupId,c.Name})排序依据(M =方式> m.Name);
    计算机[TMManagers] =新的SelectList(_TMS,ChannelGroupId,姓名);
}公众的ActionResult SummaryReport()
{
    DistrictManagerModel模式=新DistrictManagerModel();
    LoadDistrictManagers();
    返回视图(AreaManager模型);
}


解决方案

使用HTTPAttributes领域给双方的下拉列表中唯一的ID:

  @ Html.DropDownListFor(M => m.DistrictId,计算机[DMManagers]为IEnumerable< SelectListItem>中的select one,新的{@ ID =ddlDMManagers })

第二下拉应该被初始化为空列表:

  @ Html.DropDownListFor(M = GT; m.TMId,Enumerable.Empty< SelectListItem>(),新的{@ ID =ddlTMManagers})

如果你不介意使用jQuery Ajax时被触发一号下拉列表中选择变事件来更新第二个下拉菜单:

  $(函数(){
    $('#选择ddlDMManagers')。改变(函数(){
        VAR districtId = $(本).VAL();
        $阿贾克斯({
            网址:'LoadTerritoryManagers',
            输入:POST,
            数据:JSON.stringify({districtId:districtId})
            数据类型:JSON,
            的contentType:应用/ JSON,
            成功:功能(数据){
                $。每个(数据,功能(键,TMManagers){
                    $('#选择ddlTMManagers')追加。('<期权价值=0>选择的One< /选项>');
                    //通过TM经理环路和填充下拉
                    $。每个(TMManagers,函数(指数,经理){
                        $('#选择ddlTMManagers')。追加(
                            '<期权价值=+ manager.Id +'>'
                            + manager.Name +
                            '< /选项>');
                    });
                });
            }
        });
    });
});

这个类添加到您的控制器命名空间:

 公共类TMManager
{
    公众诠释标识{搞定;组;}
    公共字符串名称{;组;}
}

您将需要更新您的控制器动作,LoadTerritoryManagers(),响应Ajax请求,并返回{ID,名称}对象的JSON阵列。

  [HttpPost]
    公众的ActionResult LoadTerritoryManagers(INT districtId)
    {
        VAR _TMS =(从SessionHandler.CurrentContext.ChannelGroupsÇ
                加入CGT在SessionHandler.CurrentContext.ChannelGroupTypes上c.ChannelGroupTypeId等于cgt.ChannelGroupTypeId
                其中,cgt.Name ==领地&放大器;&安培; c.ParentChannelGroupId == districtId
                选择新TMManager(){ID = c.ChannelGroupId,名称= c.Name})排序依据(M =方式> m.Name);        如果(_TMS == NULL)
            返回JSON(NULL);        清单< TMManager>管理者=(列表< TMManager>)_ TMS.ToList();
        返回JSON(经理);
    }

I have two dropdownlist. The selected value from the first one loads the other. How do I do that when I have the helper methods in a controller?

@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, "ChannelGroupId", "Name");
}

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

解决方案

Give both dropdowns unique IDs using the HTTPAttributes field:

@Html.DropDownListFor(m => m.DistrictId, ViewData["DMManagers"] as IEnumerable<SelectListItem>, "Select One", new {@id="ddlDMManagers"})

2nd dropdown should be initialized as an empty list:

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

If you don't mind using jQuery ajax to update the 2nd dropdown when a 'change' event is triggered on the 1st dropdown:

$(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>');
                    });
                });
            }
        });
    });
});

Add this class to your controller namespace:

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

You will need to update your controller action, LoadTerritoryManagers(), to respond to the ajax request and return a JSON array of {Id,Name} objects.

    [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);
    }

这篇关于在选择更改事件 - Html.DropDownListFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆