2016-11-08 131 views
1

我有一個客戶類與屬性性別。我創建了一個性別類型列表,其中包含身份證號碼和性別類型。當提交表單時,我得到空值。Dropdownlist返回空值

查看

@model MovieRentals.ViewModel.CustomerView 
<div class="form-group"> 
    <h4>@Html.LabelFor(l => l.Customer.BirthDate)</h4> 
    @Html.DropDownListFor(l => l.CustomerGender, new SelectList(Model.CustomerGender, "GenderId", "GenderType"), "Select Gender", new { @class = "form-control" }) 
</div> 

型號

public class CustomerView 
{ 
    public IEnumerable<MembershipType> MembershipTypes{ get; set; } 
    public Customer Customer { get; set; } 
    public List<GenderClass> CustomerGender{ get; set; } 
} 

public class GenderClass 
{ 
    public int GenderId { get; set; } 
    public string GenderType { get; set; } 
} 

控制器

public ActionResult New() 
    { 
     var MembershipTy = _context.MemebershipType.ToList(); 
     var ViewModel = new CustomerView(); 
     ViewModel.CustomerGender = new List<GenderClass>() 
     { 
      new GenderClass(){ GenderId = 1, GenderType = "Male"}, 
      new GenderClass() { GenderId = 2, GenderType = "Female"} 
     }; 
     ViewModel.MembershipTypes = MembershipTy; 
     return View(ViewModel); 
    } 
+0

因爲您使用與數據源相同的屬性並保存選擇。您需要一個不可枚舉的屬性來保存選定的值。 – CodeCaster

+0

您不能將一個'