0

我有一个带有 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;
        }
4

5 回答 5

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

HTH,
查尔斯

于 2010-05-04T05:30:38.133 回答
1

看看这个博客条目。

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

基本上,您需要将您的mesurementTypeslist/enumerable 转换为 a SelectListor IEnumerable<SelectListItem>

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

于 2010-05-04T05:14:03.167 回答
0

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

如何使用 ASP.NET MVC 创建 DropDownList

于 2010-05-04T05:16:01.853 回答
0

假设 Model.Mes_Id 包含所选值,您可以执行以下操作

<%
    var Measurements = new SelectList((IEnumerable)ViewData["MeasurementType"], "Id", "Name", Model.Mes_Id);
    Response.Write(Html.DropDownList("measurement_type", Measurements, "Select"));
%>
于 2010-05-04T05:26:52.587 回答
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>
于 2013-03-06T19:07:18.193 回答