gpt4 book ai didi

asp.net-mvc-4 - DropDownlist 值作为 System.Collections.Generic.List`1[System.Web.Mvc.SelectListItem] 插入

转载 作者:行者123 更新时间:2023-12-05 06:46:03 25 4
gpt4 key购买 nike

我试图在数据库中插入下拉列表的值,但不是存储它的选定值,而是存储此值 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();
}

非常感谢任何帮助。谢谢!

最佳答案

我总是尽量避免使用 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;
}
}

然后在你的 View 中:

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

关于asp.net-mvc-4 - DropDownlist 值作为 System.Collections.Generic.List`1[System.Web.Mvc.SelectListItem] 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19181347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com