2

我正在尝试在 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();
    }

任何帮助将不胜感激。谢谢!

4

2 回答 2

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())
于 2013-10-04T12:58:02.543 回答
0

这条线很疯狂:

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

它应该是:

update.occupation = edit.propertyType;
于 2013-10-04T13:03:47.750 回答