2014-09-10 30 views
1

我想用一個ViewModel與傳遞兩個型號有一種觀點認爲

我的模型,兩個模型傳遞到一個視圖中存在的問題:

 @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> 

m y問題是linq查詢沒有給出我想要的結果 (但是如果我給了num_cin poste.id一些值,它的工作)

所以問題是,num_cin沒有從dropdownlist的價值...這就像有一個空值!!!!!!!!!

回答

0

更改POST方法簽名就可以接受的模式,以及訪問模型屬性

[HttpPost] 
public ActionResult Index(PosteCandidatViewModel model) 
{ 
    Poste poste = model.Poste; 
    string num_cin = model.Candidat.num_cin; 

之所以參數string num_cin爲空是因爲@TextBoxFor(model => model.candidat.num_cin)產生<input type="text" name="candidat.num_cin" ... />正試圖映射到包含財產屬性candidatnum_cin。另外UPI可以使用

[HttpPost] 
public ActionResult Index(Poste poste, [Bind(Prefix="candidat")]string num_cin) 
{ 

注意,如果ModelState是無效的,需要重新分配,你在DropDownListFor()

if (ModelState.IsValid) 
{ 
    .... 
} 
ViewBag.Postes = // set the value here before returning the view 
return View(model); 
+0

使用我想,它給了我一個異常ViewBag.Postes的價值.... .............值不能爲空。參數名稱:items(DrodownList) – saidmohamed11 2014-09-10 21:51:40

+0

發生異常的位置在哪裏? – 2014-09-10 21:55:55

+0

我修正了這個異常,這是因爲我在模型中有一個驗證.......我不想通過PosteCandidatViewModel作爲我Post操作的參數....因爲如果我這樣做,它會給我一些驗證錯誤(因爲我的模型會隱藏很多必需的屬性) – saidmohamed11 2014-09-10 22:13:00

相關問題