選択したドロップ ダウンリストの値を db に挿入していますが、ドロップダウン リスト ヘルパーのビューでエラーが発生しています。キー「SpaceType」を持つタイプ「IEnumerable」の ViewData アイテムはありません。モデル:
public class AddSpace
{
public string SpaceType { get; set; }
}
意見:
@Html.DropDownListFor(m => m.SpaceType, (IEnumerable<SelectListItem>)ViewData["property"])
コントローラ
[HttpGet]
public ActionResult AddSpace()
{
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;
}
[HttpPost]
public ActionResult AddSpace(AddSpace adspace)
{
if (ModelState.IsValid)
{
string userName = "wasfa_anjum@yahoo.com";
var query = from q in Session.Query<Registration>()
where q.Email == userName
select q;
Session.Store(adspace);
Session.SaveChanges();
}
else ModelState.AddModelError("","Please Correct the errors to continue");
return View();
}