Saturday, 15 August 2015

c# - Selected value is not showing for dropdownlist in MVC 4 -



c# - Selected value is not showing for dropdownlist in MVC 4 -

model

public class leadsvm { public leadsvm() { this.leadslist = new list<singleleadvm>(); } public ilist<singleleadvm> leadslist { get; set; } // backup user , assign user public ienumerable<userprofile> users { get; set; } public leaddetailsvm leaddetailsvm { get; set; } [display(name = "lead status")] public ienumerable<leadstatus> leadstatuslist { get; set; } }

view

@for (int = 0; < model.leadslist.count; i++) { @html.dropdownlistfor(model => model.leadslist[i].assignedtouserid, new selectlist(model.users, "userid", "email"), "select user", new { data_val = false, data_users = model.leadslist[i].leadid, @class = "userslist" }) }

the reason why using loop rather foreach can submit collection of info controller. problem if value can passed view controller , vice versa unable display right value in dropdown list(i can see optional label).

solution

@html.dropdownlistfor(model => model.leadslist[i].assignedtouserid, new selectlist(model.users, "userid", "email", model.leadslist[i].assignedtouserid), "select user", new { data_val = false, data_users = model.leadslist[i].leadid, @class = "userslist" })

fixed it. turned out utilize 4th parameter of selectlist constructor.

@html.dropdownlistfor(model => model.leadslist[i].assignedtouserid, new selectlist(model.users, "userid", "email", model.leadslist[i].assignedtouserid), "select user", new { data_val = false, data_users = model.leadslist[i].leadid, @class = "userslist" }) – adam bielecki 9 mins ago

c# asp.net asp.net-mvc

No comments:

Post a Comment