我在 MVC4 应用程序中工作,我需要保存下拉列表值并从数据库中检索它。我已将值保存到数据库但无法通过单击按钮检索值。
我想要什么:默认情况下,我的下拉列表有值(“男性”,“女性”,“其他”)。我选择女性并将其保存到数据库中。当我从数据库中检索时,它正在加载默认值而不显示所选值..
.CsHTML:
@Html.DropDownListFor(m => m.Gender, (IEnumerable<SelectListItem>)ViewData["GenderMaster"])
模型:
public string Gender
{
set;
get;
}
控制器:
IEnumerable<BindindClass> gender = onjLoad.LoadGenderMaster();//Default dropdown binding
ViewData["GenderMaster"] = from c in gender
select new SelectListItem
{
Text = c.Text,
Value = c.Value,
};
item = onjLoad.ReadLoadPatientData(controlID["Gender"].ToString().Replace(",",""));
return View(item);
where item is the object of model,LoadGenderMaster() is the method used to get dropdown data from databse,ReadLoadPatientData() is the method which i use to retrive saved data from database.