c# - How to create two DropDownList from differents model in the same view using ASP MVC SQL and EF -


i developing asp.net mvc 3 application using c# , sql server 2005. using entity framework code-first approach.

i have model profile_ga views(index,create,...).

i created dropdownlist in index of model load ids (id_gamme).

i want load ids of table (another model poste) in index of profil_ga.

but error appears :

databinding: 'mvcapplication2.models.profile_ga' not contain property name 'id_poste'.

this controller of profile_ga :

namespace mvcapplication2.controllers {      public class profilegacontroller : controller     {         private gammecontext db = new gammecontext();          //         // get: /profilega/          public viewresult index(profile_ga profile_ga, poste poste)         {             viewbag.id_gamme = new selectlist(db.profil_gas, "id_gamme", profile_ga.id_gamme);             viewbag.id_poste = new selectlist(db.postes, "id_poste", poste.id_poste);             return view(db.profil_gas.tolist());           } 

and add in index :

<%:html.label("gamme :")%>        <%: html.dropdownlist("id_gamme", new selectlist(model, "id_gamme", "id_gamme ")) %>           <%:html.label("poste :")%>        <%: html.dropdownlist("id_poste", new selectlist(model, "id_poste", "id_poste ")) %> 

the error spot on. model doesn't contain id_poste.

you storing db.postes viewbag model being passed view db.profil_gas in part: return view(db.profil_gas.tolist()); - won't contain db.postes.

as want show 2 separate things best approach create new viewmodel class contains both things this.

view model

public class myviewmodel {     // 2 drop-down lists     public list<profile_ga> profile_gas { get; set; }     public list<poste> postes { get; set; }      // used store selected items     public int selectedprofile_ga { get; set; }     public int selectedposte { get; set; } } 

then in controller

[httpget] public actionresult index(profile_ga profile_ga, poste poste) {     var viewmodel = new myviewmodel();     viewmodel.profile_gas = db.profil_gas.tolist();     viewmodel.postes = db.postes.tolist();     return view(viewmodel); }  [httppost] public actionresult index(myviewmodel viewmodel) {     string debug = string.format("you selected profile: {0} , poste: {1}", viewmodel.selectedprofile_ga, viewmodel.selectedposte);     return view(viewmodel); } 

finally in view

<%: html.dropdownlist("selectedprofile_ga", new selectlist(model.profile_gas, "id_gamme", "nametoshow")) %> <%: html.dropdownlist("selectedposte", new selectlist(model.postes, "id_poste", "nametoshow")) %> 

then replace nametoshow property want show in drop-down box. then, when submit form viewmodel passed value of drop down boxes (as shown in code example). place breakpoint on debug in httppost item check values right , should go!


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -