Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Getting undefined in Cascading DropDownList In ASP.Core MVC ?

Getting undefined in Cascading DropDownList In ASP.Core MVC ?

Scheduled Pinned Locked Moved ASP.NET
asp-netdatabasequestionjavascripthtml
8 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    In the first DropdownList should display the Countries and in the second should display Cities based on the Country like .. USA then display just cities in USA . well i am getting undefined in dropdown city the Question is why i am getting always undefined in City dropDown list ?

    here is countrol :

    public ActionResult Index()
    {
    Details();
    return View();
    }

        public void Details (
        {
            var model = db.GetUniversities().ToList();
            List li = new List();
            li.Add(new SelectListItem { Text = "Please select Country",Value="0"});
            foreach (var item in model)
            {
                li.Add(new SelectListItem { Text = item.Country, Value = item.Id.ToString()});
                ViewBag.state = li;
            }
        }
        \[HttpPost\]
        public JsonResult GetCity (int id)
        {
            var ddlCity = db.GetUniversities().Where(x => x.Id == id).ToList();
            List licities = new List();
    
            //licities.Add(new SelectListItem { Text = "--Select City--", Value = "0" });
    
            if (ddlCity != null)
            {
                foreach (var x in ddlCity)
                {
                    licities.Add(new SelectListItem { Text = x.City, Value = x.Id.ToString() });
                }
            }
            return Json(new SelectList(licities, "Text", "Value"));
    
        }
    

    here view

    @Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
    
    
        @Html.DropDownListFor(model => model.Country, ViewBag.state as List, new { style = "width: 200px;" })
    
        @Html.ValidationMessageFor(model => model.Country, "", new { @class = "text-danger" })
    
    
    @Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
    
    
        @Html.DropDownListFor(model => model.City, new SelectList(string.Empty, "Value", "Text"), "--Select City--", new { style = "width:200px" })
    
        @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
    

    here jQuery

    $(document).ready(function () {

        $("#Country").change(function () {
            $("#City").empty();
            $.ajax({
                type: 'POST',
    
    V 1 Reply Last reply
    0
    • L Lost User

      In the first DropdownList should display the Countries and in the second should display Cities based on the Country like .. USA then display just cities in USA . well i am getting undefined in dropdown city the Question is why i am getting always undefined in City dropDown list ?

      here is countrol :

      public ActionResult Index()
      {
      Details();
      return View();
      }

          public void Details (
          {
              var model = db.GetUniversities().ToList();
              List li = new List();
              li.Add(new SelectListItem { Text = "Please select Country",Value="0"});
              foreach (var item in model)
              {
                  li.Add(new SelectListItem { Text = item.Country, Value = item.Id.ToString()});
                  ViewBag.state = li;
              }
          }
          \[HttpPost\]
          public JsonResult GetCity (int id)
          {
              var ddlCity = db.GetUniversities().Where(x => x.Id == id).ToList();
              List licities = new List();
      
              //licities.Add(new SelectListItem { Text = "--Select City--", Value = "0" });
      
              if (ddlCity != null)
              {
                  foreach (var x in ddlCity)
                  {
                      licities.Add(new SelectListItem { Text = x.City, Value = x.Id.ToString() });
                  }
              }
              return Json(new SelectList(licities, "Text", "Value"));
      
          }
      

      here view

      @Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
      
      
          @Html.DropDownListFor(model => model.Country, ViewBag.state as List, new { style = "width: 200px;" })
      
          @Html.ValidationMessageFor(model => model.Country, "", new { @class = "text-danger" })
      
      
      @Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
      
      
          @Html.DropDownListFor(model => model.City, new SelectList(string.Empty, "Value", "Text"), "--Select City--", new { style = "width:200px" })
      
          @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
      

      here jQuery

      $(document).ready(function () {

          $("#Country").change(function () {
              $("#City").empty();
              $.ajax({
                  type: 'POST',
      
      V Offline
      V Offline
      Vincent Maverick Durano
      wrote on last edited by
      #2

      You may have to set an ID attribute to your DropDownListFor control so you can reference them in your jQuery code. For example:

      @Html.DropDownListFor(model => model.Country, ViewBag.state as List, { @id="ddlCountry", @class="YourCSSClassName" })

      then in your jQuery, you can access it like this:

      $("#ddlCountry").change(function () {

      });

      L 1 Reply Last reply
      0
      • V Vincent Maverick Durano

        You may have to set an ID attribute to your DropDownListFor control so you can reference them in your jQuery code. For example:

        @Html.DropDownListFor(model => model.Country, ViewBag.state as List, { @id="ddlCountry", @class="YourCSSClassName" })

        then in your jQuery, you can access it like this:

        $("#ddlCountry").change(function () {

        });

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        i am still getting the same error .. where should i exactly write the id .. could you more explain please i thank you very much in advanced

        V 1 Reply Last reply
        0
        • L Lost User

          i am still getting the same error .. where should i exactly write the id .. could you more explain please i thank you very much in advanced

          V Offline
          V Offline
          Vincent Maverick Durano
          wrote on last edited by
          #4

          You need to give ID's to all your controls that you reference in your jQuery and then use the IDs just like what I demonstrated. If you followed that and you are still getting error, then post your updated code here and show us which line you are getting the error.

          L 1 Reply Last reply
          0
          • V Vincent Maverick Durano

            You need to give ID's to all your controls that you reference in your jQuery and then use the IDs just like what I demonstrated. If you followed that and you are still getting error, then post your updated code here and show us which line you are getting the error.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Hi Vincent, so i am still getting the same problem thus let me show you what i have changed . i did what you have told me . could you check once again and in SQL there are some Countries and Cities like USA -> Bosten , Germany -> Frankfurt etc.... the View :

            @Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
            
            
                @Html.DropDownListFor(model => model.Country, ViewBag.state as List
            , new {@id ="ddlCountry", @class = "form-control" })
            
            @Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
            
            
                @Html.DropDownListFor(model => model.City, new SelectList(string.Empty, "Value", "Text"), "--Select City--", 
               new {@id ="ddlCity", @class = "form-control" })
            

            The jQuery :

            $(document).ready(function () {

                $("#ddlCountry").change(function () {
                    $("#ddlCity").empty();
                    $.ajax({
                        type: 'POST',
                        url: '@Url.Action("GetCity")',
                        dataType: 'json',
                        data: { id: $("#ddlCountry").val() },
                        success: function (city) {
            
                            $.each(city, function (i, city) {
                                $("#ddlCity").append(''+ city.Text + '');
            
                            });
                        },
                        error: function (ex) {
                            alert('Failed.' + ex);
                        }
                    });
                    return false;
                })
            });
            

            and with Controls haven't changed :

            public void Details ()
            {
            var model = db.GetUniversities().ToList();
            List li = new List();
            li.Add(new SelectListItem { Text = "Please select Country",Value="0"});
            foreach (var item in model)
            {
            li.Add(new SelectListItem { Text = item.Country, Value = item.Id.ToString()});
            ViewBag.state = li;
            }
            }
            [HttpPost]
            public JsonResult GetCity (int id)
            {
            var ddlCity = db.GetUniversities().Where(x => x.Id == id).ToList();
            List licities = new List();

                    licities.Add(new SelectListItem { Text = "--Select City--", Value = "0" });
            
                    if (ddlCity != null)
            
            V 1 Reply Last reply
            0
            • L Lost User

              Hi Vincent, so i am still getting the same problem thus let me show you what i have changed . i did what you have told me . could you check once again and in SQL there are some Countries and Cities like USA -> Bosten , Germany -> Frankfurt etc.... the View :

              @Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
              
              
                  @Html.DropDownListFor(model => model.Country, ViewBag.state as List
              , new {@id ="ddlCountry", @class = "form-control" })
              
              @Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
              
              
                  @Html.DropDownListFor(model => model.City, new SelectList(string.Empty, "Value", "Text"), "--Select City--", 
                 new {@id ="ddlCity", @class = "form-control" })
              

              The jQuery :

              $(document).ready(function () {

                  $("#ddlCountry").change(function () {
                      $("#ddlCity").empty();
                      $.ajax({
                          type: 'POST',
                          url: '@Url.Action("GetCity")',
                          dataType: 'json',
                          data: { id: $("#ddlCountry").val() },
                          success: function (city) {
              
                              $.each(city, function (i, city) {
                                  $("#ddlCity").append(''+ city.Text + '');
              
                              });
                          },
                          error: function (ex) {
                              alert('Failed.' + ex);
                          }
                      });
                      return false;
                  })
              });
              

              and with Controls haven't changed :

              public void Details ()
              {
              var model = db.GetUniversities().ToList();
              List li = new List();
              li.Add(new SelectListItem { Text = "Please select Country",Value="0"});
              foreach (var item in model)
              {
              li.Add(new SelectListItem { Text = item.Country, Value = item.Id.ToString()});
              ViewBag.state = li;
              }
              }
              [HttpPost]
              public JsonResult GetCity (int id)
              {
              var ddlCity = db.GetUniversities().Where(x => x.Id == id).ToList();
              List licities = new List();

                      licities.Add(new SelectListItem { Text = "--Select City--", Value = "0" });
              
                      if (ddlCity != null)
              
              V Offline
              V Offline
              Vincent Maverick Durano
              wrote on last edited by
              #6

              What error are you getting now? Still undefined? Please be specific also for the errors that you are currently getting.

              L 1 Reply Last reply
              0
              • V Vincent Maverick Durano

                What error are you getting now? Still undefined? Please be specific also for the errors that you are currently getting.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                it's fine now . i have got it . i thank you a lot

                V 1 Reply Last reply
                0
                • L Lost User

                  it's fine now . i have got it . i thank you a lot

                  V Offline
                  V Offline
                  Vincent Maverick Durano
                  wrote on last edited by
                  #8

                  That's great! Glad to be of help! :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups