asp.net mvc - Saving the value from dropdown to entity framework MVC -
i trying save values dropdownlist database using mvc 5 + entity framework. values dropdownlist taken db (and displayed), when saving web entry value of ordersourcelist
null , ordersourceid
= 0 model
public class orderfullmodel { [display(name = "ordersource")] public ilist<selectlistitem> ordersourcelist {get;set;} public int ordersourceid { get; set; } }
controller
public actionresult create() //this returning right dropdownlist { var ordersources = _repository.all<ordersource>().orderby(m => m.nameru).tolist(); viewbag.myordersources = new selectlist(ordersources, "id", "nameru", 0); return view(); }
view
@html.labelfor(m => m.ordersourcelist) @html.dropdownlistfor(m => m.ordersourcelist, (selectlist)viewbag.myordersources)
the dropdownlistfor
helper expects expression value in list want display/set, so:
@html.dropdownlistfor(m => m.ordersourceid, (selectlist)viewbag.myordersources)
currently you're asking dropdown set value of list.
from docs:
public static mvchtmlstring dropdownlistfor( htmlhelper<tmodel> htmlhelper, expression<func<tmodel, tproperty>> expression, ienumerable<selectlistitem> selectlist)
expression: expression identifies object contains properties display.
selectlist: collection of selectlistitem objects used populate drop-down list.
Comments
Post a Comment