asp.net mvc 4 - Mvc: Create method to populate dropdown list from a class and call from controller -


the code below works in index controller action populates dropdownbox using database data.

i don't want use directly in controller because using dropdown in multiple places on page.

var db = new storemanagerentities();         var query = db.categories.select(c => new         {             categoryid = c.categoryid,             categoryname = c.categoryname,             isselected = c.categoryid.equals(0)         });          var model = new selectviewmodel         {             list = query.tolist()                         .select(c => new selectlistitem                         {                             value = c.categoryid.tostring(),                             text = c.categoryname,                             selected = c.isselected,                         })         };          return view(model); 

i want able put code in method , call method controller here class want method go.

public class northwinddatacontext {      storemanagerentities mydb = new storemanagerentities();      //retrieve category objects     public list<category> getcategories()     {         return mydb.categories.tolist();     }      //populate dropdownbox         public void populatedropdown()         {               var query = db.categories.select(c => new         {             categoryid = c.categoryid,             categoryname = c.categoryname,             isselected = c.categoryid.equals(0)         });          var model = new selectviewmodel         {             list = query.tolist()                         .select(c => new selectlistitem                         {                             value = c.categoryid.tostring(),                             text = c.categoryname,                             selected = c.isselected,                         })         };          }   }    can please show me how can write method here , have    return data need controller. nice if    can show me how call controller well.  

how change northwinddatacontext class

public class northwinddatacontext     {         storemanagerentities mydb = new storemanagerentities();             //retrieve category objects         public list<category> getcategories()         {             return mydb.categories.tolist();         }             //populate dropdownbox         public selectviewmodel populatedropdown()         {             var query = db.categories.select(c => new                 {                     categoryid = c.categoryid,                     categoryname = c.categoryname,                     isselected = c.categoryid.equals(0)                 });              var model = new selectviewmodel             {                 list = query.tolist()                             .select(c => new selectlistitem                             {                                 value = c.categoryid.tostring(),                                 text = c.categoryname,                                 selected = c.isselected,                             })             };             return model;          }          } 

notice change in return type of populatedropdown() , return model;

and use in controller like:

var db = new northwinddatacontext(); var model = db.populatedropdown();  return view(model); 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -