c# - Problems with passing two models to one view -
i want pass 2 models 1 view using viewmodel
my models :
public class candidat { public int id { set; get; } public string num_cin { set; get; } public icollection<poste> postes { get; set; } } public class poste { public int id { set; get; } public string poste_name {set;get} public list<candidat> candidats {set;get;} } public class postecandidatviewmodel { public candidat candidat { get; set; } public poste poste { get; set; } }
the controller action :
[httppost] public actionresult index( poste poste,string num_cin) { if (modelstate.isvalid) { var v = (from c in _db.candidats c.num_cin == num_cin && c.postes.any(p => p.id == poste.id) select c) .singleordefault(); if (v != null) { return redirecttoaction("inscription", "candidat"); } else { return redirecttoaction("index", "bureauordre"); } } return view();
the view :
@model procrec.models.postecandidatviewmodel <td>@html.textboxfor(model => model.candidat.num_cin)</td> <td><p>@html.dropdownlistfor(model => model.poste.id,new selectlist(viewbag.postes, "id", "intitule_poste"),"choisir le poste") </p></td>
my problem linq query not giving result want (but if gave num_cin poste.id values it's work )
so problem num_cin not have valu dropdownlist ...it's having empty value!!!!!!!!!
change post method signature accept model, , access model properties
[httppost] public actionresult index(postecandidatviewmodel model) { poste poste = model.poste; string num_cin = model.candidat.num_cin;
the reason parameter string num_cin
null because @textboxfor(model => model.candidat.num_cin)
generates <input type="text" name="candidat.num_cin" ... />
which trying map property candidat
contains property num_cin
. alternatively upi can use
[httppost] public actionresult index( poste poste, [bind(prefix="candidat")]string num_cin) {
note, if modelstate
invalid, need reassign the value of viewbag.postes
use in dropdownlistfor()
if (modelstate.isvalid) { .... } viewbag.postes = // set value here before returning view return view(model);
Comments
Post a Comment