简体   繁体   中英

Inserting DropDownList value in db

I am inserting selected value of drop downlist in db but it is giving me error in view where the dropdownlist helper is. There is no ViewData item of type 'IEnumerable' that has the key 'SpaceType'. Model:

        public class AddSpace
        {
             public string SpaceType { get; set; }
        }

View:

        @Html.DropDownListFor(m => m.SpaceType,  (IEnumerable<SelectListItem>)ViewData["property"])

Controller

    [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();
    }

Have you tried [updated]:

@Html.DropDownList("SpaceTypes", ViewData["property"] as SelectList)

Also, after:

items.Add(new SelectListItem { Text = "Other", Value = "Other" });

You'll probably need to add:

items.Add(new SelectListItem { Text = "Other", Value = "Other" }, "Text", "Value", 1);

[added] Optionally, you should be able to write it like this also:

var items = new SelectList(new[]
    new { Text = "Private Residence", Value = "Private Residence" },
    new { Text = "Office", Value = "Office" },
    new { Text = "Place of worship", Value = "Place of worship" },
    new { Text = "Commercial Parking lot", Value = "Commercial Parking lot" },
    new { Text = "Retail", Value = "Retail" },
    new { Text = "Academic Institution", Value = "Academic Institution" },
    new { Text = "Other", Value = "Other" },
"Text", "Value", 1);

ViewData["property"] = items;

投射到列表:

  @Html.DropDownListFor(m => m.SpaceType, (List<SelectListItem>)ViewData["property"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM