c# - Using a model to create a drop down list -
asp.net mvc 4 entity framework 5 linq c# i have viewmodel using already. adding permissiontype model viewmodel.
this model represents table in database same name.
how turn table @html.dropdownlistfor()
from table 'text' need 'name' database. table 'value' need 'id' database.
how turn model usable drop down list controller page?
forumboard model = new forumboard(); model.tpgforumquery = db.tpgforums; model.propit_user = db.propit_user.where(m => m.username == username).firstordefault(); model.tpgforumtopicquery = db.tpgforumtopics; // need here model.permissiontype = // list of of permission in table edit:
public class forumboard { public forumboard() { this.propit_user = new propit_user(); this.tpgforum = new tpgforum(); this.permissiontype = new list<listitem>(); } list<listitem> permissiontype { get; set; } public virtual ienumerable<tpgforum> tpgforumquery { get; set; } public virtual propit_user propit_user { get; set; } public virtual ienumerable<tpgforumtopic> tpgforumtopicquery { get; set; } public virtual tpgforum tpgforum { get; set; } }
first, change view model
class forumboard { ... public ienumerable<listitem> permissiontype {get; set;} ... } then, assign
forumboard model = new forumboard(); ... model.permissiontype = db.permissiontype .select(p=> new listitem (){ text = p.fieldfortext, value= p.fieldforvalue }); edit: if fields non string type, unfortunately, have first call tolist() method.
model.permissiontype = db.permissiontype //.select(p => new {text = p.textfield, value = p.permissionid }) .tolist() .select(p=> new listitem (){ text = p.text.tostring(), value= p.value.tostring()});
Comments
Post a Comment