asp.net mvc 3 - Bind dropdown list from database -


i have been struggling create dropdown list display country names database. the situation is: have controller "advertisementcontroller", model"advertisementmodel" , view "create.cshtml". on view need create dropdown list display country names database.

i know thing create viewmodel. how shall that? bunch of code appreciated. :)

i have following code shows 'null reference' error.

viewmodel:

 public class communicationviewmodel     {          public string categoryid { get; set; }         public ienumerable<selectlistitem> categorylist { get; set; }     } 

model:

public class createadmodel     {         [required]         [display(name = "title")]         public string title { get; set; }          [required]         [display(name = "description")]         [datatype(datatype.multilinetext)]         public string message { get; set; }          [required]         [display(name = "ad type")]         public string adtype { get; set; }          [required]         [display(name = "ad category")]         public string adcategory { get; set; }          public communicationviewmodel categories { get; set; }       } 

controller:

public actionresult index()     {         var query = db.addcategory.select(c => new selectlistitem                                                  {                                                      value = c.id.tostring(),                                                      text = c.name                                                  }                                                  );         var model = new communicationviewmodel { categorylist = query.asenumerable() };         return view(model);     } 

razor:

 @html.dropdownlistfor(m=>m.categories.categoryid,model.categories.categorylist,"--select one--") 

this may you. drop down roles when adding users. simple tutorial

http://rtur.net/blog/post/2009/06/03/quick-and-dirty-role-management-in-aspnet-mvc.aspx


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -