我成功地将值保存到数据库(标题值)上,但是当我在编辑模式下渲染相同的视图时,标题字段必须保存选定的值,但在我的情况下标题下拉列表中没有选择任何值...不知道为什么我在标题字段保存存储值(在后端)时没有选择任何内容的下拉列表。Html.DropDownListFor在编辑模式下未选择的值
@Html.DropDownListFor(model => model.title, new SelectList(Model.titles, "Value", "Text"),"-Select-") // nothing selected on edit mode
@Model.title //displaying the stored value which the user selected initially.
值标题
titles = new SelectList(ListItem.getValues().ToList(), "Value", "Text").ToList();
getValue函数
public static List<TextValue> getValues()
{
List<TextValue> titles= new List<TextValue>();
TextValue T= new TextValue();
T.Value = "Mr";
T.Text = "Mr";
titles.Add(T);
T= new TextValue();
T.Value = "Mrs";
T.Text ="Mrs";
titles.Add(T);
T= new TextValue();
T.Value = "Miss";
T.Text = "Miss";
titles.Add(T);
T= new TextValue();
T.Value ="Other";
T.Text = "Other";
titles.Add(T);
return titles;
}
可能你正在将char()保存到数据库中,如果是这样,那么在编辑模式下,你会得到空格和文本......检查是否是这种情况。 – RollerCosta
不,我正在使用nvarchar() – Ankit