【问题标题】:How to make dropdownlist show a selected value in asp.net mvc?如何使下拉列表在 asp.net mvc 中显示选定的值?
【发布时间】:2011-02-15 07:28:37
【问题描述】:

我有一个带有 Html.DropDownList 的编辑页面......我无法显示下拉列表值,它总是以Select 显示,而是我想让下拉列表显示基于模型值选择的项目说Model.Mes_Id...任何建议如何完成...

       <p>
            <label for="MeasurementTypeId">MeasurementType:</label>
         <%= Html.DropDownList("MeasurementType", // what should i give here?)%>
            <%= Html.ValidationMessage("MeasurementTypeId", "*") %>
        </p>

编辑:它有列表项,但我想显示在编辑视图中选择的值...

   public ActionResult Edit(int id)
    {
        var mesurementTypes = consRepository.FindAllMeasurements();
        ViewData["MeasurementType"] = mesurementTypes;
        var material = consRepository.GetMaterial(id);
        return View("Edit", material);
    }

我的存储库方法,

public IEnumerable<SelectListItem> FindAllMeasurements()
        {
            var mesurements = from mt in db.MeasurementTypes
                              select new SelectListItem
                              {
                                 Value = mt.Id.ToString(),
                                 Text= mt.Name
                              };
            return mesurements;
        }

【问题讨论】:

  • 请详细说明。您发布的代码 完全符合它的要求。创建一个带有一个选项的
  • @john 考虑我的下拉列表包含 9 个项目....Model.Mes_Id=5 我想在我的编辑视图中选择值为 5 的 MeasurementType http://localhost:1985/Materials/Edit/13..

标签: asp.net-mvc drop-down-menu findby


【解决方案1】:

在创建IEnumerable&lt;SelectListItem&gt;时设置选中项。

我个人会为表单创建一个专门的视图模型,但按照您的代码,执行以下操作:

public ActionResult Edit(int id)
{
    //Put this first
    var material = consRepository.GetMaterial(id);
    //pass in your selected item
    var mesurementTypes = consRepository.FindAllMeasurements(material.MeasurementTypeId);
    ViewData["MeasurementType"] = mesurementTypes;

    return View("Edit", material);
}

然后将您的存储库方法更改为:

public IEnumerable<SelectListItem> FindAllMeasurements(int selectedId)
{
     var mesurements = from mt in db.MeasurementTypes
                      select new SelectListItem
                      {
                         Value = mt.Id.ToString(),
                         Text= mt.Name,
                         Selected = mt.Id == selectedId
                      };

    return mesurements;
}

HTH,
查尔斯

【讨论】:

  • @Charlino 有效.....它适用于编辑,但此方法将如何用于添加我不想要选定值的视图...
  • 如果您只传入 0,则不应选择任何值 - 假设您没有 id 为 0 的 MeasurementType! :-)
【解决方案2】:

看看这个博客条目。

http://weblogs.asp.net/ashicmahtab/archive/2009/03/27/asp-net-mvc-html-dropdownlist-and-selected-value.aspx

基本上,您需要将mesurementTypes 列表/可枚举转换为SelectListIEnumerable&lt;SelectListItem&gt;

如果可能,我建议升级到 ASP.NET MVC2 并使用 Html.DropDownListFor()

【讨论】:

  • @alastair 我在我的存储库类中使用IEnumerable&lt;SelectListItem&gt; 那么为什么我不能设置...
【解决方案3】:

您应该返回一个可以指定所选项目的 SelectionList。

How to create a DropDownList with ASP.NET MVC

【讨论】:

    【解决方案4】:

    假设 Model.Mes_Id 包含选定的值,您可以这样做

    <%
        var Measurements = new SelectList((IEnumerable)ViewData["MeasurementType"], "Id", "Name", Model.Mes_Id);
        Response.Write(Html.DropDownList("measurement_type", Measurements, "Select"));
    %>
    

    【讨论】:

      【解决方案5】:

      Html.DropDownListFor 对我不起作用所以我得到了这样的罂粟

          (in Edit method )
      
          CreatList(long.Parse(wc.ParentID.ToString()));
      
      
          private void CreatList(long selected= 0)
          {
              SqlConnection conn = new SqlConnection(Config.ConnectionStringSimple);
              conn.Open();
              Category wn = new Category(conn);
              CategoryCollection coll = new CategoryCollection();
              Category.FetchList(conn, ref coll);
      
              ViewBag.ParentID = GetList(coll, selected);   
          }
      
      
          private List<SelectListItem> GetList(CategoryCollection coll, long selected)
          {
              List<SelectListItem> st = new List<SelectListItem>();
              foreach (var cat in coll)
              {
                  st.Add( new SelectListItem
                  {
                      Text = cat.Name,
                      Value = cat.ID.ToString(),
                      Selected = cat.ID == selected
                  });
      
              }
              SelectListItem s = new SelectListItem { 
                                      Text = Resources.lblSelect, 
                                      Value = "0" 
              };
              st.Insert(0, s);
              return st;
          }
      
      
          <div class="editor-label">
              @Html.LabelFor(model => model.ParentID)
          </div>
          <div class="editor-field">
              @Html.DropDownList("ddlCat", (List<SelectListItem>)ViewBag.ParentID)
              @Html.ValidationMessageFor(model => model.ParentID)
          </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-05
        • 2014-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-14
        相关资源
        最近更新 更多