级联下拉菜单MVC 3 [英] Cascading dropdowns MVC 3

查看:169
本文介绍了级联下拉菜单MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在MVC 3,创建级联下拉列表逻辑学是简单的,每个位置可能有很多员工,所以最好的办法我能想出如何实现它的下降downlists。

我现在所拥有的是:

1列表是在与位置begninnging填满。和变化,我可以将数据放入第二个我的员工名单,但我传送数据是Enumrable日期。

当我尝试发送了员工与ID和名称没有happend。

控制器code:

 公众的ActionResult月(字符串LOCID)
{
  VAR K = service.getEmployeeForLocation(Int32.Parse(LOCID))
                 .ToList()
                 。选择(X =>新建
                         {
                           值= .EmployeeId,
                           文字= x.Name
                         });  返回JSON(K,JsonRequestBehavior.AllowGet);
}

查看:

 < TR>
< TD>选择离您最近的位置:
    @ Html.DropDownListFor(X => x.SelectedLocation,Model.Locations)LT; / TD>
< TD>选择离您最近的位置:
    @ Html.DropDownListFor(X => x.SelectedEmployee,
                          Enumerable.Empty< SelectListItem>() - 选择月 - )
< / TR>

的JavaScript

 < / SCRIPT>
<脚本类型=文/ JavaScript的>
    $('#SelectedLocation')。改变(函数(){
        VAR selectedLocation = $(本).VAL();
        如果(selectedLocation = NULL&放大器;!&安培;!selectedLocation =''){
            $ .getJSON(@ Url.Action(月)',{LOCID:selectedLocation},
            功能(员工){                VAR EmployeeSelect = $('#SelectedEmployee');
                // monthsSelect.empty();                $。每个(员工,函数(指数,员工){
                    EmployeeSelect.append($('<选项/>',{
                        值:employee.value,
                        文本:employee.text
                    }));
                });
            });        }
    });
< / SCRIPT>


解决方案

有一件事,我可以看到的是,在你的行动开始以大写字母和JavaScript的变量名是区分大小写的。在你的页面中,您使用的是小写,但如果你想保持外壳在控制器二人应该是大写的。

I am about to create cascading dropdown lists in MVC 3. Logik is simple, for each location there may be many Employees, so best way i could figure out how to implement it is drop downlists.

what i have right now is :

1 list is filled up at begninnging with Locations. and on change i can get data into second my Employee list, but the data i transfer is Enumrable dates".

As soon as i try to send Employees over with ID and name nothing happend.

Controller code:

public ActionResult Months(string locId)
{
  var k = service.getEmployeeForLocation(Int32.Parse(locId))
                 .ToList()
                 .Select(x => new
                         {
                           Value = .EmployeeId,
                           Text = x.Name 
                         });

  return Json(k,JsonRequestBehavior.AllowGet); 
}

View :

<tr>
<td>Choose your closest location : 
    @Html.DropDownListFor(x => x.SelectedLocation, Model.Locations)</td>
<td>Choose your closest location : 
    @Html.DropDownListFor(x => x.SelectedEmployee, 
                          Enumerable.Empty<SelectListItem>(), "-- select month --")
</tr>

Javascript

</script>
<script type="text/javascript">
    $('#SelectedLocation').change(function () {
        var selectedLocation = $(this).val();
        if (selectedLocation != null && selectedLocation != '') {
            $.getJSON('@Url.Action("Months")', { locId: selectedLocation }, 
            function (employee) {

                var EmployeeSelect = $('#SelectedEmployee');
                //                monthsSelect.empty();

                $.each(employee, function (index, employee) {
                    EmployeeSelect.append($('<option/>', {
                        value: employee.value,
                        text: employee.text
                    }));
                });
            });

        }
    });
</script>

解决方案

One thing that I can see is that your variable names in your action start with an upper case and JavaScript is case sensitive. In your page you are using lowercase but they two should be upper case if you want to keep the casing in your controller.

这篇关于级联下拉菜单MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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