c# - problems with getting data for dropdownlist -
i have dropdownlist getting data data base
it contains 4 data :
my controller :
public actionresult index() { viewbag.postes = _db.postes.tolist(); return view(); } [httppost] public actionresult index( poste poste,string num_cin="r346399") { if (modelstate.isvalid) { if (poste.id == 3) { return redirecttoaction("inscription", "candidat"); } return view(poste); } }
my view
@html.dropdownlistfor(model => model.id, new selectlist(viewbag.postes, "id", "intitule_poste"),"choisir le poste")
the problem if choose value dropdownlist !=3 it's give me error "that items must not null "
you view includes @html.dropdownlistfor()
generated based on value of viewbag.postes
. when return view (i.e. when poste.id not equal 3) must reassign value of viewbag.postes
if (modelstate.isvalid) { if (poste.id == 3) { return redirecttoaction("inscription", "candidat"); } viewbag.poste = _db.postes.tolist(); // reassign collection dropdown return view(poste); }
Comments
Post a Comment