DropDownlist值作为System.Collections.Generic.List`1 [System.Web.Mvc.SelectListItem]插入

时间:2013-10-04 12:37:07

标签: asp.net-mvc-4 drop-down-menu

我正在尝试在db中插入dropdownlist的值,而不是存储其选定的值 存储此值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();
    }

任何帮助都将受到高度赞赏。谢谢!

2 个答案:

答案 0 :(得分:1)

我总是尽量避免使用ViewData或ViewBag。请为此目的尝试以下:

为所有下拉列表创建静态类:

public static class MyDrops
{

public static List<SelectListItem> GetList()
{
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" });
return items;
}
}

然后在你的视图中:

@Html.DropDownListFor(m =>m.propertyType, MyDrops.GetList())

答案 1 :(得分:0)

这条线很疯狂:

update.occupation = ViewData["property"].ToString();

它应该是:

update.occupation = edit.propertyType;