mvc dropdow更改不会触发

时间:2016-11-29 08:53:09

标签: asp.net-mvc cascadingdropdown

我尝试在mvc页面上使用3个下拉列表,如果我更改ddl 1,ddl 2中的值应该更改,并且更改ddl 2应该更改ddl 3中的值。到目前为止,我有这个代码.. ..设置了ddl 1的值,但如果我在ddl 1中更改值,则不会发生任何事情。 ddl 2没有得到任何值和"公共JsonResult GetPapperByTypeId(int typeId)"根本没有触发。

我在这里缺少什么?

主页视图

                            @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "myForm", name = "papper_search" }))
                            {

                                            @Html.DropDownList("PrintType", ViewData["papperType"] as List<SelectListItem>, new { @class = "form-control" })
                                            <br />
                                            <br />
                                            @Html.DropDownList("Papper", new SelectList(string.Empty, "Value", "Text"), "Please select a paper", new { @class = "form-control" })

                                            <br />
                                            <br />
                                            @Html.DropDownList("PapperType", new SelectList(string.Empty, "Value", "Text"), "Please select a type", new { @class = "form-control" })

                            }





<script type="text/javascript" src="@Url.Content("~/js/jquery.min.js")"></script>
<script type="text/jscript">
$(function ()
{
    $('#PrintType').change(function ()
    {
        $.getJSON('/Home/GetPapperByTypeId/' + $('#PrintType').val(), function (data)
        {
            var items = '<option>Select Papper</option>';
            $.each(data, function (i, printtype)
            {
                items += "<option value='" + printtype.Value + "'>" + printtype.Text + "</option>";
            });
            $('#Papper').html(items);
        });
    });

    $('#Papper').change(function ()
    {
        $.getJSON('/Home/Citylist/' + $('#Papper').val(), function (data)
        {
            var items = '<option>Select PapperType</option>';
            $.each(data, function (i, pappertype)
            {
                items += "<option value='" + pappertype.Value + "'>" + pappertype.Text + "</option>";
            });
            $('#PapperType').html(items);
        });
    });
});
</script>

家庭控制器

   public ActionResult Index()
    {

        var li = new List<SelectListItem>
        {
            new SelectListItem {Text = "Select", Value = "0"},
            new SelectListItem {Text = "Plain paper", Value = "1"},
            new SelectListItem {Text = "Heavy paper", Value = "2"},
        };
        ViewData["papperType"] = li;

    }

    //Action result for ajax call
    public JsonResult GetPapperByTypeId(int typeId)
    {
        var objallPappers = GetAllPappers().Where(m => m.TypeId == typeId).ToList();
        var obgpapper = new SelectList(objallPappers, "Id", "Name", 0);
        return Json(obgpapper, JsonRequestBehavior.AllowGet);
    } 


    public List<Papper> GetAllPappers()
    {
        var objPapper = new List<Papper>
        {
            new Papper {Id = 1, TypeId = 1, Name = "papper-1"},
            new Papper {Id = 2, TypeId = 2, Name = "papper-1"},
            new Papper {Id = 3, TypeId = 4, Name = "papper-1"},
            new Papper {Id = 4, TypeId = 1, Name = "papper-2"},
            new Papper {Id = 5, TypeId = 1, Name = "papper-3"},
            new Papper {Id = 6, TypeId = 4, Name = "papper-2"}
        };
        return objPapper;
    }               

1 个答案:

答案 0 :(得分:1)

问题:就在这行代码中

$.getJSON('/Home/GetPapperByTypeId/' + $('#PrintType').val(), function (data)

实际问题是/Home/GetPapperByTypeId/' + $('#PrintType').val()

最终会出现/Home/GetPapperByTypeId/SomeValue

这样的网址

解决方案:您想要的网址是/Home/GetPapperByTypeId/?typeId=SomeValue

请注意?typeId = 已添加到查询字符串中。因为控制器Action需要名为typeId

的参数
public JsonResult GetPapperByTypeId(int typeId)

因此,将jquery中的语法更改为

$.getJSON('/Home/GetPapperByTypeId/?typeId=' + $('#PrintType').val(), function (data)

您在此行代码中遇到的类似问题。

$.getJSON('/Home/Citylist/' + $('#Papper').val(), function (data)也解决了这个问题。