下拉菜单未选择正确的值
Dropdown not selecting correct value
以下代码有什么问题?
@Html.DropDownListFor(model => model.title, new List<SelectListItem>
{
new SelectListItem { Text = "Other" , Value = "Other"},
new SelectListItem { Text = "Mr", Value = "Mr" },
new SelectListItem { Text = "Mrs", Value = "Mrs" },
new SelectListItem { Text = "Ms", Value = "Ms" },
new SelectListItem { Text = "Miss", Value = "Miss" }
},
new { @class = "form-control" })
上面允许我 select 并将值保存到 table,但是当涉及到编辑时,保存的值不是 selected
例如,现有数据在编辑时使用 "Mr" 保存,显示 "Other"
为什么?
尝试使用 SelectList
with this overload 以下方式:
@Html.DropDownListFor(model => model.title, new SelectList(new List<SelectListItem>
{
new SelectListItem { Text = "Other" , Value = "Other"},
new SelectListItem { Text = "Mr", Value = "Mr" },
new SelectListItem { Text = "Mrs", Value = "Mrs" },
new SelectListItem { Text = "Ms", Value = "Ms" },
new SelectListItem { Text = "Miss", Value = "Miss" }
},
"Value",
"Text",
Model.Title),
new { @class = "form-control" })
SelectList Constructor (IEnumerable, String, String, String, Object)
public SelectList(
IEnumerable items,
string dataValueField,
string dataTextField,
string dataGroupField,
Object selectedValue
)
这是最糟糕的
它在这个原因中起作用,但不是上面的那个
@Html.DropDownListFor(model => model.PreferredContact, new List<SelectListItem>
{
new SelectListItem { Text = "Other" },
new SelectListItem { Text = "Telephone", Value = "Telephone" } ,
new SelectListItem { Text = "Email", Value = "Email" },
new SelectListItem { Text = "Post", Value = "Post" }
}, new { @class = "form-control" })
好的
我发现了问题
您不能使用 "title" 作为名称。标题是 HTML5 上的 属性。我将该字段重命名为 dTitle 并且有效
以下代码有什么问题?
@Html.DropDownListFor(model => model.title, new List<SelectListItem>
{
new SelectListItem { Text = "Other" , Value = "Other"},
new SelectListItem { Text = "Mr", Value = "Mr" },
new SelectListItem { Text = "Mrs", Value = "Mrs" },
new SelectListItem { Text = "Ms", Value = "Ms" },
new SelectListItem { Text = "Miss", Value = "Miss" }
},
new { @class = "form-control" })
上面允许我 select 并将值保存到 table,但是当涉及到编辑时,保存的值不是 selected
例如,现有数据在编辑时使用 "Mr" 保存,显示 "Other"
为什么?
尝试使用 SelectList
with this overload 以下方式:
@Html.DropDownListFor(model => model.title, new SelectList(new List<SelectListItem>
{
new SelectListItem { Text = "Other" , Value = "Other"},
new SelectListItem { Text = "Mr", Value = "Mr" },
new SelectListItem { Text = "Mrs", Value = "Mrs" },
new SelectListItem { Text = "Ms", Value = "Ms" },
new SelectListItem { Text = "Miss", Value = "Miss" }
},
"Value",
"Text",
Model.Title),
new { @class = "form-control" })
SelectList Constructor (IEnumerable, String, String, String, Object)
public SelectList(
IEnumerable items,
string dataValueField,
string dataTextField,
string dataGroupField,
Object selectedValue
)
这是最糟糕的
它在这个原因中起作用,但不是上面的那个
@Html.DropDownListFor(model => model.PreferredContact, new List<SelectListItem>
{
new SelectListItem { Text = "Other" },
new SelectListItem { Text = "Telephone", Value = "Telephone" } ,
new SelectListItem { Text = "Email", Value = "Email" },
new SelectListItem { Text = "Post", Value = "Post" }
}, new { @class = "form-control" })
好的
我发现了问题
您不能使用 "title" 作为名称。标题是 HTML5 上的 属性。我将该字段重命名为 dTitle 并且有效