如何让dropdownlist在asp.net mvc中显示一个选定的值?

时间:2010-05-04 04:38:39

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

我有一个带有Html.DropDownList的编辑页面....我无法显示它总是显示Select的下拉列表值而不是我想让下拉列表显示基于a选择的项目模型值说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;
        }

5 个答案:

答案 0 :(得分:2)

创建IEnumerable<SelectListItem>时设置所选项目。

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

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;
}

HTHS,
查尔斯

答案 1 :(得分:1)

查看此博客条目。

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

基本上,您需要将mesurementTypes列表/枚举转换为SelectListIEnumerable<SelectListItem>

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

答案 2 :(得分:0)

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

How to create a DropDownList with ASP.NET MVC

答案 3 :(得分:0)

假设Model.Mes_Id包含所选值,您可以执行类似这样的操作

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

答案 4 :(得分:0)

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>