加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 在编辑模式下未选择的Html.DropDownListFor值

发布时间:2020-12-16 09:48:19 所属栏目:asp.Net 来源:网络整理
导读:我成功地能够在插入时将值保存到数据库(标题值),但是当我在编辑模式下渲染相同的视图时,标题字段必须保持选定的值,但在我的情况下,没有通过标题下拉列表选择值…不知道为什么我在标题字段保存存储值(在后端)时没有选择任何内容的下拉列表. @Html.DropDownLis
我成功地能够在插入时将值保存到数据库(标题值),但是当我在编辑模式下渲染相同的视图时,标题字段必须保持选定的值,但在我的情况下,没有通过标题下拉列表选择值…不知道为什么我在标题字段保存存储值(在后端)时没有选择任何内容的下拉列表.

@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(),"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;

   }

解决方法

你必须使用SelectList的另一个ctor

从msdn起

SelectList(IEnumerable,String,Object)

Initializes a new instance of the SelectList class by using the
specified items for the list,the data value field,the data text
field,and a selected value.

然后 :

@Html.DropDownListFor(model => model.title,"Text",Model.title),"-Select-")

顺便说一句,遵循基本标准(至少)通常是一个好主意:您的属性应该以大写字母char开头.

public string Title {get;set;}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读