我正在尝试在 db 中插入下拉列表的值,但不是存储其选定的值,而是存储此值System.Collections.Generic.List1[System.Web.Mvc.SelectListItem]
。这是我的代码。看法:
@Html.DropDownListFor(m =>m.propertyType, (List<SelectListItem>) ViewData["property"])
模型:
public string propertyType { get; set; }
[HttpGet]
public ActionResult EditProfile()
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "Private Residence", Value = "Private Residence" });
items.Add(new SelectListItem { Text = "Office", Value = "Office" });
items.Add(new SelectListItem { Text = "Place of worship", Value = "Place of worship" });
items.Add(new SelectListItem { Text = "Commercial Parking lot", Value = "Commercial Parking lot" });
items.Add(new SelectListItem { Text = "Retail", Value = "Retail" });
items.Add(new SelectListItem { Text = "Academic Institution", Value = "Academic Institution" });
items.Add(new SelectListItem { Text = "Other", Value = "Other" });
ViewData["property"] = items;
return View();
}
[HttpPost]
public ActionResult EditProfile(EditProfile edit)
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "Private Residence", Value = "Private Residence" });
items.Add(new SelectListItem { Text = "Office", Value = "Office" });
items.Add(new SelectListItem { Text = "Place of worship", Value = "Place of worship" });
items.Add(new SelectListItem { Text = "Commercial Parking lot", Value = "Commercial Parking lot" });
items.Add(new SelectListItem { Text = "Retail", Value = "Retail" });
items.Add(new SelectListItem { Text = "Academic Institution", Value = "Academic Institution" });
items.Add(new SelectListItem { Text = "Other", Value = "Other" });
ViewData["property"] = items;
string UserName = User.Identity.Name;
var query = from q in Session.Query<Registration>()
where q.Email == UserName
select q;
if (query.Count() > 0)
{
foreach (var update in query)
{
update.contactNo = edit.contactNo;
update.occupation = ViewData["property"].ToString();
}
}
else ModelState.AddModelError("","");
Session.SaveChanges();
return View();
}
任何帮助将不胜感激。谢谢!