c# - Mvc List Strongly type View DropDownListFor not showing current value -
c# - Mvc List Strongly type View DropDownListFor not showing current value -
i have next dropdownlistfor
control
@html.dropdownlistfor(m => item.relationshipid, new selectlist(viewbag.relationship,"id", "title"), new { @id = "ddl_" + @item.userid, @class = "form-control", onchange = "update(this,'" + @item.userid + "','" + @item.sentbyuserid + "');" })
i bind type view @model list<mvcui.models.userdetails>
in viewbag.relationship
have next list
public static list<idtitledto> datatabletorelationshiplist(this datatable table) { var list = table.asenumerable() .select(dr => new idtitledto { id = dr.field<int>("relationshipid"), title = dr.field<string>("relationshipname") }).tolist(); homecoming list; }
i passing list of userdetails
model view. here userdetails
model
public class userfrienddetails { public string userid { get; set; } public string username { get; set; } public string firstname { get; set; } public int? relationshipid { get; set; } .... ... other fields }
suppose in list have 3 record. out of them 2 records relationshipid null. if it's null setting -1 -- select --. in remaining 1 record have relationshipid suppose have 13. when view opens binding -- select -- first 2 record. 3rd record not binding appropriate value viewbag. can't figure out issue. can 1 help me solve this?
edithere have created 1 simple action i.e. user
[allowanonymous] public actionresult user() { var model = new list<userdetails>(); model.add(new userdetails { id = 1, fname = "ajay", relationid = 3}); model.add(new userdetails { id = 2, fname = "vijay", relationid = 1 }); model.add(new userdetails { id = 3, fname = "john", relationid = 2 }); var rlist = new list<idtitledto>(); rlist.add(new idtitledto { id = 1 , title = "m"}); rlist.add(new idtitledto { id = 2, title = "f" }); rlist.add(new idtitledto { id = 3, title = "b" }); viewbag.rel = rlist; homecoming view(model); }
here user.cshtml
@model list<webapplicationdropdown.models.userdetails> <table class="table"> <tr> <th> fname </th> <th></th> </tr> @*@foreach (var item in model) {*@ @for (int = 0; < model.count(); i++) { <tr> <td> @html.displayfor(modelitem => model[i].fname) </td> <td> @html.dropdownlistfor(modelitem => model[i].relationid, new selectlist(viewbag.rel, "id", "title"), "--select--") </td> </tr> } </table>
in view not binding relationid. default showing -- select --
instead of showing next relation
ajay - b vijay - m john - f
please see next image
new editeditor template userdetails.cshtml
@model webapplicationdropdown.models.userdetails @html.dropdownlistfor(m => m.relationid, (selectlist)viewdata["relationshiplist"])
view.cshtml
@model ienumerable<webapplicationdropdown.models.userdetails> <table class="table"> <tr> <th> fname </th> <th></th> </tr> @foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.fname) </td> <td> @*@html.dropdownlistfor(modelitem => item.relationid, new selectlist(viewbag.rel, "id", "title"), "--select--")*@ @html.editorfor(m => m, new { relationshiplist = new selectlist(viewbag.rel, "id", "title") }) </td> </tr> } </table>
please see next screenshot
create custom editortemplate
type of userfrienddetails
in /views/shared/editortemplates/userfrienddetails.cshtml
@model userfrienddetails @html.textboxfor(m => m.username) .... @html.dropdownlstfor(m => m.relationshipid, (selectlist)viewdata["relationshiplist"])
then in main view, pass selectlist
template using additional view data
@model ienumerable<userfrienddetails> .... @html.editorfor(m => m, new { relationshiplist = new selectlist(viewbag.relationship,"id", "title") })
this generate right name attributes bind collection, example
<select name="[0].relationshipid" ..> <select name="[1].relationshipid" ..>
and select right alternative based on value of each relationshipid
property.
side note: suggest remove new { @id = "ddl_" + @item.userid,...
, utilize default id
generated helper. not clear onchange
attribute doing suggest utilize unobtrusive javascript rather polluting markup behavior.
c# asp.net-mvc html.dropdownlistfor
Comments
Post a Comment