drop down menu - Asp.Net MVC3 - How create Dynamic DropDownList -


i found many articles on still don´t know how this. trying create own blog engine, have view create article (i using ef , code first) , must fill number of category in article should add want change dropdownlist names of categories. model looks this:

public class article {     public int articleid { get; set; }     [required]     public string title { get; set; }     [required]     public int categoryid { get; set; }     public datetime date { get; set; }     [required()]     [datatype(datatype.multilinetext)]     [allowhtml]     public string text { get; set; }     public virtual category category { get; set; }     public ienumerable<selectlistitem> categories { get; set; }     public virtual icollection<comment> comments { get; set; } } public class category {     public int categoryid { get; set; }     [required]     public string name { get; set; }     public virtual icollection<article> articles { get; set; }  } 

i know must use enum (or think) not sure how. don´t know tutorial found best me.


edit:

thanks answers found else. trying this:

this model:

public class article {     [key]     public int articleid { get; set; }      [display(name = "title")]     [stringlength(30, minimumlength = 5)]     [required]     public string title { get; set; }      public datetime date { get; set; }      public int categoryid { get; set; }      [required()]     [datatype(datatype.multilinetext)]     [allowhtml]     public string text { get; set; }     public category category { get; set; }      public virtual icollection<comment> comments { get; set; }      public ienumerable<category> categories { get; set; } } public class category { [key]     public int categoryid { get; set; }     [required] public string categoryname { get; set; }     public virtual icollection<article> articles { get; set; }  } 

this controller create article:

public actionresult vytvorit() {     ienumerable<category> categories = getcaregories();     var view = view(new article() { categories = categories });     view.tempdata.add("action", "create");      return view;  }  private static ienumerable<category> getcaregories() {     ienumerable<category> categories;     using (blogdbcontext context = new blogdbcontext())     {         categories = (from 1 in context.categories                       orderby one.categoryname                       select one).tolist();     }     return categories; }  private category getcategory(int categoryid) {         return db.categories.find(categoryid); } // // post: /clanky/vytvorit  [httppost] public actionresult vytvorit(article newarticle) {      try     {         if (newarticle.categoryid > 0)         {             newarticle.category = getcategory(newarticle.categoryid);         }         if (tryvalidatemodel(newarticle))         {                 db.articles.add(newarticle);                 db.savechanges();             return redirecttoaction("index");         }         else         {             newarticle.categories = getcaregories();             var view = view(newarticle);             view.tempdata.add("action", "create");             return view;         }     }     catch     {         return view();      } } 

and part of view:

     @html.dropdownlistfor(model => model.categoryid, new selectlist(model.categories,"categoryid","categoryname"))         @html.validationmessagefor(model => model.categoryid) 

i have problem nullreferenceexeption don´t know why. can way? looks easy me.

your model seems quite strange. contains properties such categoryid , category seem redundant. contains selectlistitem collection property called categories. so, model or view model? looks quite messed up. let's assume it's model. in case more this:

public class article {     public int articleid { get; set; }      [required]     public string title { get; set; }      public datetime date { get; set; }      [required()]     [datatype(datatype.multilinetext)]     [allowhtml]     public string text { get; set; }      public virtual category category { get; set; }      public ienumerable<category> categories { get; set; }      public virtual icollection<comment> comments { get; set; } }  public class category {     public int categoryid { get; set; }      [required]     public string name { get; set; }      public virtual icollection<article> articles { get; set; }  } 

now model clear define view model passed view. view model class designed view. depending on intend put in view define in view model. far have talked drop down, let's it:

public class articleviewmodel {     public int selectedcategoryid { get; set; }     public ienumerable<selectlistitem> categories { get; set; } } 

and have controller:

public class articlescontroller: controller {     private readonly iarticlesrepository _repository;     public articlescontroller(iarticlesrepository repository)     {         _repository = repository;     }      public actionresult index()     {         article article = _repository.getarticle();         articleviewmodel viewmodel = mapper.map<article, articleviewmodel>(article);         return view(viewmodel);     } } 

so controller uses repository fetch model, maps view model (in example use automapper) , passes view model view take care of showing it:

@model appname.models.articleviewmodel @using (html.beginform()) {     @html.dropdownlistfor(         x => x.selectedcategoryid,          new selectlist(model.categories, "value", "text"),         "-- select category --"     )     <input type="submit" value="ok" /> } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -