Search code examples
asp.net-mvccascadingdropdown

mvc dropdow change does not trigger


I try to use 3 dropdown lists on a mvc page, if I change ddl 1 the values in ddl 2 should change, and a change of ddl 2 should change the values in ddl 3. So far I have this code.... the values for ddl 1 are set but if I change value in ddl 1 nothing happends. The ddl 2 does not get any values and the "public JsonResult GetPapperByTypeId(int typeId)" is not triggered at all.

What am I missing here?

Home view

                            @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>

Home controller

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

Solution

  • Problem: is in this line of code

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

    Actual problem is this /Home/GetPapperByTypeId/' + $('#PrintType').val()

    This would end up with a URL like /Home/GetPapperByTypeId/SomeValue

    Solution: What you want is the URL to be like /Home/GetPapperByTypeId/?typeId=SomeValue

    note the ?typeId= added into the query string. Because your controller Action expects a parameter with name typeId

    public JsonResult GetPapperByTypeId(int typeId)
    

    So change the syntax in your jquery to

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

    Similar issue you have in this line of code.

    $.getJSON('/Home/Citylist/' + $('#Papper').val(), function (data) fix this too..