c# - Creating Drop down List for Create Action -
i have problem creating drop down list in view create action. view show field student number, title , description , nothing program , category drop-down list. not sure how create selectlist , pass view. don't know if did right.
public class serviceform { [required] [display(name="student number")] public int student_number { get; set; } [required] [display(name="program")] public selectlist program { get; set; } [required] [display(name = "title")] public string title { get; set; } [required] [display(name = "description")] public string description { get; set; } [required] [display(name = "category")] public selectlist category { get; set; } } public class program { public int id { get; set; } public string program_code { get; set; } public string program_desc { get; set; } } public ienumerable<programlist> getallprograms() { var s = ds.programs.orderby(m => m.program_code); return automapper.mapper.map<ienumerable<programlist>>(s); } private servicemanager m = new servicemanager(); private programmanager pm = new programmanager(); [httpget] public actionresult create() { serviceform form = new serviceform(); form.program = new selectlist(pm.getallprograms(), "id", "program_code"); list<selectlistitem> cat = new list<selectlistitem>(); cat.add(new selectlistitem() { text = "general", value = "general", selected = true }); cat.add(new selectlistitem() { text = "grades", value = "grades", selected = false }); cat.add(new selectlistitem() { text = "time table", value = "timetable", selected = false }); cat.add(new selectlistitem() { text = "zenit account", value = "zenit", selected = false }); form.program = new selectlist(cat, cat.elementat(0)); return view(form); } [httppost] public actionresult create(serviceadd newservice) { if (!modelstate.isvalid) { var reform = automapper.mapper.map<serviceform>(newservice); reform.program = new selectlist(pm.getallprograms(), "id", "program_code"); list<selectlistitem> cat = new list<selectlistitem>(); cat.add(new selectlistitem() { text = "general", value = "general", selected = true }); cat.add(new selectlistitem() { text = "grades", value = "grades", selected = false }); cat.add(new selectlistitem() { text = "time table", value = "timetable", selected = false }); cat.add(new selectlistitem() { text = "zenit account", value = "zenit", selected = false }); reform.program = new selectlist(cat, cat.elementat(0)); return view(reform); } return redirecttoaction("create"); }
model
public class serviceform { [required] [display(name="student number")] public int student_number { get; set; } [required] [display(name="program")] public selectlist programs { get; set; } public string selectedprogram { get; set; } [required] [display(name = "title")] public string title { get; set; } [required] [display(name = "description")] public string description { get; set; } [required] [display(name = "category")] public selectlist categories{ get; set; } public string selectedcategory { get; set; } }
controller
[httpget] public actionresult create() { serviceform form = new serviceform(); list<selectlistitem> cat = new list<selectlistitem>(); cat.add(new selectlistitem { text = "general", value = "general", selected = true }); cat.add(new selectlistitem { text = "grades", value = "grades", selected = false }); cat.add(new selectlistitem { text = "time table", value = "timetable", selected = false }); cat.add(new selectlistitem { text = "zenit account", value = "zenit", selected = false }); form.categories = cat; //fill programs property here... return view(form); }
view
@model serviceform @using(html.beginform(...)) { @html.labelfor(m=>m.programs) @html.dropdownfor(x => x.selectedprogram , model.programs) @html.labelfor(m=>m.categories) @html.dropdownfor(x => x.selectedcategory, model.categories) }
Comments
Post a Comment