我有一個客戶類與屬性性別。我創建了一個性別類型列表,其中包含身份證號碼和性別類型。當提交表單時,我得到空值。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);
}
因爲您使用與數據源相同的屬性並保存選擇。您需要一個不可枚舉的屬性來保存選定的值。 – CodeCaster
您不能將一個'
選擇標籤'name'屬性必須與您的對象中的屬性相匹配,只有該值由MVC框架正確綁定。因此,請檢查您的選擇標籤的名稱,並確保您的模型中具有相同的屬性名稱。 –