-1

I am currently following the tutorial in order to create cascading drop down list in asp.net mvc.

https://chsakell.com/2013/09/25/master-detail-dropdown-lists-and-partial-views-with-jquery-ajax-in-mvc/

However the first drop down box is not loaded with the manufacturer data. I assume that there is an error with the JSON? I can't figure out what is wrong with the code.

Controller:

 public ActionResult ManufacturerList()
        {
            var manufacturers = Models.Manufacturer.GetManufacturers();

            if (HttpContext.Request.IsAjaxRequest())
            {
                return Json(new SelectList(manufacturers.ToArray(), "ManCode", "ManName"), JsonRequestBehavior.AllowGet);
            }

        return RedirectToAction("Index");
    }

In the model:

class Manufacturer
    {
        public string ManCode { get; set; }
        public string ManName { get; set; }

        public static IQueryable<Manufacturer> GetManufacturers()
        {
            return new List<Manufacturer>
{
new Manufacturer {
ManCode = "AC",
ManName = "ACER"
},
}.AsQueryable();
        }
    }

In the View:

@using (Html.BeginForm("Index", "Home", FormMethod.Post,

                new

                {

                    id = "OrderLaptopFormID"

                }))

{

    <fieldset>

        <legend>Make an order</legend>

        <div>

            <label for="Manufacturers">Manufacturer</label>

        </div>

        <select id="ManufacturersID" name="Manufacturers"></select>

                </fieldset>

    <input type="submit" value="Return" id="SubmitID" />

}



<script src="@Url.Content("~/Scripts/jquery-1.8.2.min.js")" type="text/javascript"></script>

<script src="@Url.Content("~/Scripts/GetManufacturer.js")"></script>

JS File:

    $(document).ready(function () {
    var URL = 'home/ManufacturerList/';
    $.getJSON(URL, function (data) {
        var items = '<option value="">Select Manufacturer</option>';
        $.each(data, function (i, manufacturer) {
            items += "<option value='" + manufacturer.Value + "'>" + manufacturer.Text
            + "</option>";
        });
        $('#ManufacturersID').html(items);
    });
});

From this code I would assume the manufacturer drop down list should be populated, but it is not. Really appreciate anyones help as new to MVC and JSON. Thanks. Appreciate your time.

Update: So when I debug I get the following error: Unhandled exception at line 1, column 1 in http://localhost:42523/Scripts/countryState.js 0x800a1391 - JavaScript runtime error: '$' is undefined

Sam
  • 149
  • 2
  • 15
  • Where/how specifically does this fail? When you debug this, what happens? Does the browser's debugging console show any errors? Is the AJAX request made? Does the server respond with the expected data? Where does the problem happen? – David Apr 20 '17 at 14:11
  • @David When I debug this the page loads but the drop down list for the manufacture is not populated with data. Just a blank drop down box appears. The console does not return any errors. I assume that the problem is with the controller or the getmanufacturer.js. – Sam Apr 20 '17 at 15:23
  • @David I placed a breakpoint in the following line ( in the controller, Manufacturer ActionMethod) return Json(new SelectList(manufacturers.ToArray(), "ManCode", "ManName"), JsonRequestBehavior.AllowGet); and the hit count is zero (break always) – Sam Apr 20 '17 at 15:24
  • Don't assume. Validate. So you placed a breakpoint, was it reached? What did the server return to the client-side code? When you debug that client-side code, what happens there? – David Apr 20 '17 at 15:27
  • @David so from what I understand the breakpoint that I placed in the controller was reached. I placed a breakpoint in the getmanufacturer.js (JSON code) and the breakpoint was not reached so I assume the error is here? – Sam Apr 20 '17 at 18:12
  • @David I think the error resides on the first line of the json code in getmanufacturer.js – Sam Apr 20 '17 at 18:14
  • Why do you think that? What indication do you have to suggest the error is there? Also, when you talk about breakpoints in your JavaScript code, what debugger are you using? I'm not certain Visual Studio is always up to the task on that. Your browser's debugging tools would be ideal for in-browser debugging. If your browser's debugging tools indicate that the JavaScript code isn't executing *at all* then clearly there's something wrong with the logic here, or some error you aren't telling us. – David Apr 20 '17 at 18:16
  • @David I am getting the following error:Unhandled exception at line 1, column 1 in ~/Scripts/countryState.js 0x800a1391 - JavaScript runtime error: '$' is undefined – Sam Apr 21 '17 at 15:22
  • That error means that you're trying to use jQuery without (or at least before) including it on the page. – David Apr 21 '17 at 15:25

1 Answers1

0

Hard to tell where the problem is, but off the bat, your Manufacturer class does not have Value or Text properties. Instead, it has ManCode and ManName.

Try:

    $.each(data, function (i, manufacturer) {
        items += "<option value='" + manufacturer.ManCode + "'>" + manufacturer.ManName
        + "</option>";
    });

Also, no need to return IQueryable, simply return IEnumerable. So no need to .AsQueryable():

   public static IEnumerable<Manufacturer> GetManufacturers()
   {
       return new List<Manufacturer> {
            new Manufacturer {
                ManCode = "AC",
                ManName = "ACER"
           }
       };
   }

Finally, don't return a SelectList from the method, simply return the list

        if (HttpContext.Request.IsAjaxRequest())
        {
            return Json(manufacturers, JsonRequestBehavior.AllowGet);
        }
Andy T
  • 10,223
  • 5
  • 53
  • 95
  • Thanks but unfortunately that did not work. Any other ideas? Or can you suggest a tutorial for cascading drop down list using json in asp.net mvc. thanks for your time. – Sam Apr 20 '17 at 15:29
  • Updated answer with other changes – Andy T Apr 20 '17 at 15:56
  • thanks for the update, but it did not work, I placed an breakpoint on the first line of the json code and the break was not reached so I assume the error is in the first line of the getmanufacturer.js? – Sam Apr 20 '17 at 18:15