gpt4 book ai didi

c# - 如何在下拉列表mvc中设置选定的值

转载 作者:行者123 更新时间:2023-11-30 17:40:48 25 4
gpt4 key购买 nike

我在 View 中有一个下拉菜单

@Html.DropDownList("GenderAllowed", (SelectList)ViewBag.LocGenederAllowedList, new { @class = "form-control" })

我正在通过 ViewBag 发送下拉列表,并通过模型发送需要在下拉列表中选择的值。

但下拉列表中的值未被选中。

我的 Controller

[HttpGet]
public ActionResult EditVendorLocation(int VendorLocationID)
{
VendorLocationHandler obj = new VendorLocationHandler();
LocationGrid objLoc = new LocationGrid();
FillGenederAllowed(objLoc);
objLoc = obj.GetVendorLocationForAdmin(VendorLocationID);

return View(objLoc);
}

Viewbag 函数

public void FillGenederAllowed(LocationGrid V)
{
Dictionary<int, string> LocGenederAllowed = EnumHelper.GetGenderStates();
SelectList LocGenederAllowedList = new SelectList(LocGenederAllowed, "key", "value");
ViewBag.LocGenederAllowedList = LocGenederAllowedList;
}

最佳答案

您传递给 DropDownList 的 SelectListItems 具有属性 Selected。在您的 ViewModel 中,将最初应选择的项目设置为 true。

关于c# - 如何在下拉列表mvc中设置选定的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33888898/

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