以下代码出了什么问题?
@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" })
以上是允许我选择并将值保存到表中,但是当它进行编辑时,未选择保存的值
例如,当编辑时,现有数据与“Mr”一起保存,显示“其他”
为什么?
答案 0 :(得分:0)
尝试SelectList
使用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构造函数(IEnumerable,String,String,String,Object)
public SelectList(
IEnumerable items,
string dataValueField,
string dataTextField,
string dataGroupField,
Object selectedValue
)
答案 1 :(得分:0)
这是最糟糕的
它正在这个原因,但不是上面的那个
@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" })
答案 2 :(得分:0)
好的
我发现了问题
你不能拥有&#34; title&#34;作为名字。 title是HTML5上的属性。我将该字段重命名为dTitle并且可以正常工作