我試圖插入dropdownlist的值在數據庫中,但不是存儲其選定的值,它是 存儲此值System.Collections.Generic.List1[System.Web.Mvc.SelectListItem]
。 這是我的代碼。 查看:DropDownlist值插入爲System.Collections.Generic.List`1 [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();
}
任何幫助將是非常appreciated.Thanks!
你的'編輯'對象是否得到正確的值? – AthibaN
是的,所有的編輯對象都得到了正確的值,除了這個下拉列表值 – Wasfa