gpt4 book ai didi

asp.net-mvc-4 - MVC DropDownList 未选择设置

转载 作者:行者123 更新时间:2023-12-01 14:05:34 25 4
gpt4 key购买 nike

我有一个用于更新操作的 View ,我的模型有一个 enum 属性:

public enum DeliveryStatusType
{
Active = 1,
Deactive = 2,
}

基本上,我填充 enum 并使用 DropDownList。为了解发生了什么,我对其进行了简化并尽可能简单地发布在此处。

我已经尝试了 Html.DropDownListHtml.DropDownListFor,这里是 View :

第一个例子(这里不设置selected):

@Html.DropDownListFor(model => model.DeliveryStatus, new SelectListItem[] {
new SelectListItem {
Selected = false,
Text = "Active Delivery",
Value = "1" },
new SelectListItem {
Selected = true,
Text = "Deactive Delivery",
Value = "2" }}) //Notice that I manually set the 2nd true

第二个例子(这里不设置selected):

@Html.DropDownList("DeliveryStatus", new SelectListItem[] {
new SelectListItem {
Selected = false,
Text = "Active Delivery",
Value = "1" },
new SelectListItem {
Selected = true,
Text = "Deactive Delivery",
Value = "2" }}) //Notice that I manually set the 2nd true

但是,我意识到当我设置 SELECT html 元素的名称和 id other than DeliveryStatus(比方说DeliveryStatus1),它神奇地起作用了!

第三个例子(在此处设置选中):

@Html.DropDownList("DeliveryStatus1", new SelectListItem[] {
new SelectListItem {
Selected = false,
Text = "Active Delivery",
Value = "1" },
new SelectListItem {
Selected = true,
Text = "Deactive Delivery",
Value = "2" }}) //Notice that I manually set the 2nd true

但是当我这样做并设置为 DeliveryStatus1 时,我无法将数据发布Controller

这有什么问题或我做错了什么?我需要两者都能够设置所选数据并将其发回

最佳答案

感谢 Stephen Muecke,对于 enums,我将值指定为 Enum.ToString() 而不是 Integer 值。

@Html.DropDownListFor(model => model.DeliveryStatus, new SelectListItem[] {
new SelectListItem {
Selected = false,
Text = "Active Delivery",
Value = "Active" },
new SelectListItem {
Selected = true,
Text = "Deactive Delivery",
Value = "Deactive" }})

关于asp.net-mvc-4 - MVC DropDownList 未选择设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26314006/

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