2012-11-14 57 views
1

當我提交時,我得到StateID始終是空的。@ Html.DropDownList選擇的值問題

任何線索我在這裏錯過了什麼?

謝謝!

HTML:

<div style="display: inline-block;">@Html.LabelFor(x => x.Common.StateID , new { @class = "SmallInput2" })</div> 
<div style="display: inline-block;">@Html.DropDownList("StateID", Model.Common.StateList, "-- Select --", new { style = "width: 130px;" })</div> 

代碼:

public ActionResult SignUp() 
     { 
      var model = new SignUpModel 
      { 
       Common = new Common(), 
       Individual = new Individual(), 
       Business = new Business() 
      }; 

      var stateList = new SelectList(_context.States.OrderBy(w => w.StateName), "ID", "StateName", _context.States.OrderBy(w => w.StateName).FirstOrDefault().ID); 
      model.Common.StateList = stateList; 
      model.Common.StateID = _context.States.OrderBy(w => w.StateName).FirstOrDefault().ID; 

      return View(model); 
     } 

型號:

[Display(Name = "State")] 
public Guid StateID { get; set; } 

[Display(Name = "States")] 
public SelectList StateList { get; set; } 
+1

您是否像預期的那樣得到模型集的其他屬性? – horgh

+0

@KonstantinVasilcov是的。所有其他禮儀都是正確的。 –

+1

我認爲它可以與'StateId'這個名字連接起來......我猜如果你使用了'DropDownListFor',它會有所不同。所以,活頁夾無法綁定它...它應該有一些前綴...檢查生成的HTML其他屬性。如果你使用'..EditorFor'輔助方法,你會看到它...... – horgh

回答

0

我在這裏找到正確的解決方案@Html.DropDownList selected value issue

所以在我的情況下,它應該是這樣的:

<div style="display: inline-block;">@Html.DropDownListFor(x => x.Common.StateID, Model.Common.StateList, "-- Select --", new { style = "width: 130px;" })</div>