我无法弄清楚为什么 Posted viewmodel 在 DropdownListFor 中没有选择的值。相反,它在其中显示 Id。
在控制器 HttpGet 编辑操作中:
model.MaritalStatuses = new SelectList(maritalStatuses, "Key", "Value", 0);
---
---
static Dictionary<int, string> maritalStatuses = new Dictionary<int, string>()
{
{0, "--Select One---"},
{1, "Unmarried,"},
{2, "Divorced, "},
{3, "Widowed, "},
{4, "Separated,"},
{5, "Annulled "}
};
看法 :
@Html.DropDownListFor(model => model.MaritalStatus, Model.MaritalStatuses,"--Select One--" , new { @class = "form-control" })
在控制器 HttpPost 编辑操作中:
public ActionResult Edit(ProfileViewModel model)
{
---
// Here I get Keys in Property instead of Values in DropdownListFor
//For example : MaritalStatus =2
---
}
在 ProfileViewModel 中:
public class ProfileViewModel
{
---
---
public string MaritalStatus { get; set; }
public SelectList MaritalStatuses { get; set; }
---
---
}
任何帮助?