How do I use the DropDownList in ASP.NET MVC 3 -
i new mvc , trying populate dropdownlist in view list of 'rules' controller. when way listed, dropdownlist bunch of items cellularautomata.models.rules. know doing incorrectly, i'm wondering how show rule description each rule in dropdownlist.
i have model
public class rule { public int id { get; set; } public int name { get; set; } public string description{ get; set; } public rule(int name, string description) { name = name; description = description; } public rule() { name = 0; description = ""; } }
a controller
public actionresult index() { var rules = rule in db.rules select rule; return view(rules.tolist()); }
and view
@model ienumerable<cellularautomata.models.rule> @{ viewbag.title = "index"; } <h2>index</h2> <table> <tr> <td> @html.dropdownlist("test", new selectlist(model)) </td> </tr> </table>
you have view model:
public class myviewmodel { public string selectedruleid { get; set; } public ienumerable<rule> rules { get; set; } }
and in controller:
public actionresult index() { var model = new myviewmodel { rules = db.rules }; return view(model); }
and in view:
@model cellularautomata.models.myviewmodel @{ viewbag.title = "index"; } <h2>index</h2> @html.dropdownlistfor( x => x.selectedruleid, new selectlist(model.rules, "id", "description") )
Comments
Post a Comment