as know can pass data controller views using viewdata , viewbag, tempdata ,session , model. used model send list of dynamically created list object view.
model :
public class person { public int id { get; set; } public string name { get; set; } public string family { get; set; } public string link { get; set; } } controller :
list<person> lstperson = new list<person>(); lstperson.add(new person() { id = 1, name = "n1", link = "l1" }); lstperson.add(new person() { id = 2, name = "n2", link = "l2" }); lstperson.add(new person() { id = 3, name = "n3", link = "l3" }); lstperson.add(new person() { id = 4, name = "n4", link = "l4" }); return view(lstperson); fill dropdownlist binding model list.
@model list<mvcapplication2.models.person> <h2>index</h2> @html.beginform("send model"){ @html.dropdownlist( "foo", new selectlist( model.select(x => new { value = x.id, text = x.name }), "value", "text" ) ) <input type="submit" value="send" /> } sending list of model possible using @html.actionlink("index","index",lstperson), how know item selected? best way catch model that's selected in dropdownlist , send controller , work selected model in list?
model:
public class peopleviewmodel { public list<selectlistitem> people {get;set;} public int selectedpersonid {get;set;} } controller:
public actionresult people() { list<person> lstperson = new list<person>(); lstperson.add(new person() { id = 1, name = "n1", link = "l1" }); lstperson.add(new person() { id = 2, name = "n2", link = "l2" }); lstperson.add(new person() { id = 3, name = "n3", link = "l3" }); lstperson.add(new person() { id = 4, name = "n4", link = "l4" }); tempdata["peoplelist"] = lstperson; var model = new peopleviewmodel { people = lstperson.select( p => new selectlistitem{ value = p.id.tostring(), text = p.name}).tolist() } return view(model); } view:
@model peopleviewmodel <h2>index</h2> @html.beginform("send model"){ @html.dropdownlistfor(m=>m.selectedpersonid,model.people) ) <input type="submit" value="send" /> } controller post action
[httppost] public actionresult people(peopleviewmodel model) { var peoplelist = (list<person>)tempdata["peoplelist"]; var selectedperson = peoplelist.firstordefault(p=>p.id == model.selectedpersonid); } send list of model possible using @html.actionlink("index","index",lstperson).
actually wrong assumption:
html.actionlinkgenerates link :<a href="">- only inputs posted controller, if want post should rendered
<input>or<select>
Comments
Post a Comment