ajax - MVC cascading dropdown -


i have 2 tables manufacturer table , manufacturermodel table. trying populate 2 dropdown lists. list of manufacturers , based off of manufacturer selected bring list of models. implemented doesn't work. doesn't anything.

i created new viewmodel incorporating 2 models

public class manufacturermodeldd {     public dbset<manufacturer> manufacturers { get; set; }     public dbset<manufacturermodel> manufacturermodels { get; set; } } 

and have created 2 functions in controller want them in.

manufacturermodeldd mm = new manufacturermodeldd();      public jsonresult getmanufacturers()     {         return json(mm.manufacturers.tolist(), jsonrequestbehavior.allowget);     }      public jsonresult getmodelsbymanufacturerid(string manufacuterid)     {         int id = convert.toint32(manufacuterid);          var models = in mm.manufacturermodels a.manufacturerid == id select a;          return json(models);     } 

in view have

<script> $(function(){ $.ajax({     type: "get",     url: "/device/getmanufacturers",     datatype: "json",     success: function (data) {         $.each(data, function (index, value) {             $('#dropdownmanufacturer').append('<option value="' + value.manufacturerid + '">' +             value.manufacturer1 +'</option>');         });     } });      $('#dropdownmanufacturer').change(function(){         $('#dropdownmodel').empty();          $.ajax({             type: "post",             url: "/device/getmodelsbymanufacturerid",             datatype: "json",             data: { manufacturerid: $('#dropdownmanufacturer').val() },             success: function (data) {                 $.each(data, function (index, value) {                     $('#dropdownmodel').append('<option value="' + value.manufacturerid + '">' +                         value.model + '</option>');                 });             }         });     });     });    

manufacturermodeldd class not inherit dbcontext. not sure object of class can used access entities.

you may consider createing object of db context class , access entity data.
assuming db context class looks this

public class yourdbcontext : dbcontext {     public dbset<manufacturer> manufacturers { get; set; }     public dbset<manufacturermodel> manufacturermodels { get; set; } } 

you may use object of class.

public jsonresult getmanufacturers() {     var db = new yourdbcontext();     var data = db.manufacturers                .select(s=> new {id =s.manufacturerid ,                                                  name = s.manufacturername}).tolist();     return json(data , jsonrequestbehavior.allowget); } 

and make sure use correct properties when looping through data.

success: function (data) {     $.each(data, function (index, value) {         $('#dropdownmanufacturer').append('<option value="' + value.id+ '">' +         value.name+'</option>');     }); } 

also, make sure have view specific javascript code under scrips section

@section scripts {   <script>      $(function(){          //your code goes here.      });   </script> } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -