MVC 5 Update Dropdown from Partial with AJax [duplicate]












0















This question already has an answer here:




  • Dynamically populate the drop-down using jQuery in ASP.Net MVC3

    2 answers




After the Ajax fires the controller action FilterEngine(), it passes the ViewBag.EngineID to the partial view. When I debug the partial, I can see the ViewData dictionary. However, the Dropdown is not updated.



What am I doing wrong?



View...



...
@Html.DropDownList("ManufacturerId", null, htmlAttributes: new { @class = "form-control-sm", id = "MFGDropdown" })
...
@{Html.RenderPartial("_engines");}


_engines.cshtml



@Html.DropDownList("EngineId", null, htmlAttributes: new { @class = "form-control-sm" })


JQuery



$("#MFGDropdown").change(function () {
var val = $(this).val();

$.ajax({
url: '/Admin/FilterEngine',
type: 'GET',
dataType: 'html',
cache: false,
data: { mfg: val },
success: function (response) {

},
error: function (x, e) {
if (x.status == 0) {
alert('You are offline!!n Please Check Your Network.');
} else if (x.status == 404) {
alert('Requested URL not found.');
} else if (x.status == 500) {
alert('Internel Server Error.');
} else if (e == 'parsererror') {
alert('Error.nParsing JSON Request failed.');
} else if (e == 'timeout') {
alert('Request Time out.');
} else {
alert('Unknow Error.n' + x.responseText);
}
}
});


AdminController



public ActionResult FilterEngine(string mfg)
{
var engines = db.Engines.Where(m => m.Manufacturer == mfg).Select(s => new SelectListItem { Text = s.Model, Value = s.Id.ToString() }).ToList();
ViewBag.EngineId = engines;
return PartialView("_engines");
}









share|improve this question















marked as duplicate by Tetsuya Yamamoto, Community Nov 21 at 17:33


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    0















    This question already has an answer here:




    • Dynamically populate the drop-down using jQuery in ASP.Net MVC3

      2 answers




    After the Ajax fires the controller action FilterEngine(), it passes the ViewBag.EngineID to the partial view. When I debug the partial, I can see the ViewData dictionary. However, the Dropdown is not updated.



    What am I doing wrong?



    View...



    ...
    @Html.DropDownList("ManufacturerId", null, htmlAttributes: new { @class = "form-control-sm", id = "MFGDropdown" })
    ...
    @{Html.RenderPartial("_engines");}


    _engines.cshtml



    @Html.DropDownList("EngineId", null, htmlAttributes: new { @class = "form-control-sm" })


    JQuery



    $("#MFGDropdown").change(function () {
    var val = $(this).val();

    $.ajax({
    url: '/Admin/FilterEngine',
    type: 'GET',
    dataType: 'html',
    cache: false,
    data: { mfg: val },
    success: function (response) {

    },
    error: function (x, e) {
    if (x.status == 0) {
    alert('You are offline!!n Please Check Your Network.');
    } else if (x.status == 404) {
    alert('Requested URL not found.');
    } else if (x.status == 500) {
    alert('Internel Server Error.');
    } else if (e == 'parsererror') {
    alert('Error.nParsing JSON Request failed.');
    } else if (e == 'timeout') {
    alert('Request Time out.');
    } else {
    alert('Unknow Error.n' + x.responseText);
    }
    }
    });


    AdminController



    public ActionResult FilterEngine(string mfg)
    {
    var engines = db.Engines.Where(m => m.Manufacturer == mfg).Select(s => new SelectListItem { Text = s.Model, Value = s.Id.ToString() }).ToList();
    ViewBag.EngineId = engines;
    return PartialView("_engines");
    }









    share|improve this question















    marked as duplicate by Tetsuya Yamamoto, Community Nov 21 at 17:33


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      0












      0








      0








      This question already has an answer here:




      • Dynamically populate the drop-down using jQuery in ASP.Net MVC3

        2 answers




      After the Ajax fires the controller action FilterEngine(), it passes the ViewBag.EngineID to the partial view. When I debug the partial, I can see the ViewData dictionary. However, the Dropdown is not updated.



      What am I doing wrong?



      View...



      ...
      @Html.DropDownList("ManufacturerId", null, htmlAttributes: new { @class = "form-control-sm", id = "MFGDropdown" })
      ...
      @{Html.RenderPartial("_engines");}


      _engines.cshtml



      @Html.DropDownList("EngineId", null, htmlAttributes: new { @class = "form-control-sm" })


      JQuery



      $("#MFGDropdown").change(function () {
      var val = $(this).val();

      $.ajax({
      url: '/Admin/FilterEngine',
      type: 'GET',
      dataType: 'html',
      cache: false,
      data: { mfg: val },
      success: function (response) {

      },
      error: function (x, e) {
      if (x.status == 0) {
      alert('You are offline!!n Please Check Your Network.');
      } else if (x.status == 404) {
      alert('Requested URL not found.');
      } else if (x.status == 500) {
      alert('Internel Server Error.');
      } else if (e == 'parsererror') {
      alert('Error.nParsing JSON Request failed.');
      } else if (e == 'timeout') {
      alert('Request Time out.');
      } else {
      alert('Unknow Error.n' + x.responseText);
      }
      }
      });


      AdminController



      public ActionResult FilterEngine(string mfg)
      {
      var engines = db.Engines.Where(m => m.Manufacturer == mfg).Select(s => new SelectListItem { Text = s.Model, Value = s.Id.ToString() }).ToList();
      ViewBag.EngineId = engines;
      return PartialView("_engines");
      }









      share|improve this question
















      This question already has an answer here:




      • Dynamically populate the drop-down using jQuery in ASP.Net MVC3

        2 answers




      After the Ajax fires the controller action FilterEngine(), it passes the ViewBag.EngineID to the partial view. When I debug the partial, I can see the ViewData dictionary. However, the Dropdown is not updated.



      What am I doing wrong?



      View...



      ...
      @Html.DropDownList("ManufacturerId", null, htmlAttributes: new { @class = "form-control-sm", id = "MFGDropdown" })
      ...
      @{Html.RenderPartial("_engines");}


      _engines.cshtml



      @Html.DropDownList("EngineId", null, htmlAttributes: new { @class = "form-control-sm" })


      JQuery



      $("#MFGDropdown").change(function () {
      var val = $(this).val();

      $.ajax({
      url: '/Admin/FilterEngine',
      type: 'GET',
      dataType: 'html',
      cache: false,
      data: { mfg: val },
      success: function (response) {

      },
      error: function (x, e) {
      if (x.status == 0) {
      alert('You are offline!!n Please Check Your Network.');
      } else if (x.status == 404) {
      alert('Requested URL not found.');
      } else if (x.status == 500) {
      alert('Internel Server Error.');
      } else if (e == 'parsererror') {
      alert('Error.nParsing JSON Request failed.');
      } else if (e == 'timeout') {
      alert('Request Time out.');
      } else {
      alert('Unknow Error.n' + x.responseText);
      }
      }
      });


      AdminController



      public ActionResult FilterEngine(string mfg)
      {
      var engines = db.Engines.Where(m => m.Manufacturer == mfg).Select(s => new SelectListItem { Text = s.Model, Value = s.Id.ToString() }).ToList();
      ViewBag.EngineId = engines;
      return PartialView("_engines");
      }




      This question already has an answer here:




      • Dynamically populate the drop-down using jQuery in ASP.Net MVC3

        2 answers








      c# jquery asp.net-mvc partial-views






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 3:15









      Tetsuya Yamamoto

      14.3k41939




      14.3k41939










      asked Nov 21 at 2:27









      Beengie

      7712825




      7712825




      marked as duplicate by Tetsuya Yamamoto, Community Nov 21 at 17:33


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Tetsuya Yamamoto, Community Nov 21 at 17:33


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          you are getting data but you need to add those options in your dropdown in your ajax success.



          these answers might help you:
          Populating dropdown with JSON result - Cascading DropDown using MVC3, JQuery, Ajax, JSON



          ASP.NET MVC Html.DropDownList populated by Ajax call to controller?






          share|improve this answer




























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            you are getting data but you need to add those options in your dropdown in your ajax success.



            these answers might help you:
            Populating dropdown with JSON result - Cascading DropDown using MVC3, JQuery, Ajax, JSON



            ASP.NET MVC Html.DropDownList populated by Ajax call to controller?






            share|improve this answer


























              0














              you are getting data but you need to add those options in your dropdown in your ajax success.



              these answers might help you:
              Populating dropdown with JSON result - Cascading DropDown using MVC3, JQuery, Ajax, JSON



              ASP.NET MVC Html.DropDownList populated by Ajax call to controller?






              share|improve this answer
























                0












                0








                0






                you are getting data but you need to add those options in your dropdown in your ajax success.



                these answers might help you:
                Populating dropdown with JSON result - Cascading DropDown using MVC3, JQuery, Ajax, JSON



                ASP.NET MVC Html.DropDownList populated by Ajax call to controller?






                share|improve this answer












                you are getting data but you need to add those options in your dropdown in your ajax success.



                these answers might help you:
                Populating dropdown with JSON result - Cascading DropDown using MVC3, JQuery, Ajax, JSON



                ASP.NET MVC Html.DropDownList populated by Ajax call to controller?







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 at 2:49









                Poonam Hirpara

                3115




                3115















                    Popular posts from this blog

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python