c# - Call a controller method to fill a dropdownlist in a view with an HTML helper -
i have code in asp.net view:
@html.dropdownlistfor(x => x.transporterid, new list<selectlistitem>, "")</p>
which leaves me empty select list.
in controller, have method returns list object want fill dropdownlist with.
<p>transporter: @html.dropdownlistfor(x => x.transporterid, @html.action("methodname", "controllername"), "")</p>
but doesn't work. thought html.action helper supposed used kind of situation, wrong. can tell me how fill dropdownlist controller method? prefer not utilize viewbag or viewdata methods, have been instructed avoid those.
the list of items beingness used populate page belongs on model.
this can list<selectlistitem>
, doesn't need be. can collection dropdownlist
can bind. example, if model has list<string>
of values build dropdownlist
, might this:
@html.dropdownlistfor(x => x.transporterid, new selectlist(model.somelistofstrings), "")
ideally model isn't dependent on things selectlist
or selectlistitem
, contains info model. view bind ui elements data. having collection of simple info types on model gets translated selectlist
in view way go.
the point model carries info controller view. not other helper methods on controller. maintain in mind adage should "keep models heavy , controllers light." controller routing requests logic , logic responses. logic belongs on model.
in absence of meaningful model view, things viewbag
or tempdata
can provide handy stand-ins. in ideal situation there strongly-typed model, if it's custom view model specific view.
c# asp.net asp.net-mvc-5
No comments:
Post a Comment