asp.net mvc - Pass data to view using action C# MVC -


i'm working on project in have number of similar models. made them inherit same base model , made common controller (basic stuff list, adding, editing etc.) handles base model. works , made things easy us, have small problem dropdowns.

without going details, make things easy me if pass data controller view using action. this:

@html.dropdownlistfor(model => model.xtypechild, action("getlist", "xcontroller")) 

where action getlist returns ienumerable<selectlistitem>

this way write code once , use number of different controllers.

i know sounds crazy, if tried use 1 of more conventional ways end redundant code again , lose of advantage gained inheriting controllers.

the alternative can see right make getlist return json items , populate dropdowns using ajax/js. make action returns partial view containing dropdown, i'm not sure if that's idea.


thank far input. in end went following solution:

we had services class handling database operations our models. add method generates ienumerable<selectlistitem> base services class. pass items drop downs through viewbag (though forces me cast them ienumerable before use in view). assign them viewbag in overridden constructor of inherited controller.

i've best consult original author of code if solution ok our project, though on holiday right now. have do. on side method produced little code, should easy replace (if have so).


the code evolved little bit , have in view (note int? casting there because property of type int , wouldn't work otherwise):

html.dropdownlistfor(         model => model.xid,          new selectlist((list<x>)viewbag.xs, "id", "name", (model != null ? (int?)model.xid : null)),             " -- select -- ",             null) 

and inside of overridden constructor of controller:

viewbag.xs = db.xs.tolist(); 

it's definitelly not best way implement dropdowns in razor, best way implement them in project. reason was: there no time make project use viewmodels , override actions of base controller handle them (as provide data dropdowns in actions).


the code evolved further. it's not idea execute queries in controller constructor if data used in handful of views. constructor contains equivalent of:

viewbag.xs = (iqueryable<x>)db.xs; 

and view:

html.dropdownlistfor(         model => model.xid,          new selectlist((iqueryable<x>)viewbag.xs, "id", "name", (model != null ? (int?)model.xid : null)),             " -- select -- ",             null) 

this way query executed when view needs data.

items drop down lists should in viewmodel. don't think it's wrong create select lists in derived viewmodels. might seem duplicate code, @ least it's clear do. can create nice helpers in base controller takes care of mapping entities selectlistitems.

if want generic solution, make method in base model:

public class baseviewmodel {     // other properties..      public ienumerable<selectlistitem> getlistfromaction(string actionname, string controllername)     {         // return select list action.     } } 

and use in dropdownlistfor():

@html.dropdownlistfor(model => model.xtypechild, model.getlistfromaction("getlist", "xcontroller")) 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -