我有一个带有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;
        }
有帮助吗?

解决方案

创建所选项目 IEnumerable<SelectListItem>.

我个人会为该表单创建一个专门的ViewModel,但按照您的代码进行操作,请执行类似的操作:

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,
查尔斯

其他提示

看看这个博客条目。

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

基本上,您需要转换您的 mesurementTypes 列表/枚举 SelectList 或者 IEnumerable<SelectListItem>.

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

您应该返回可以指定选定项目的选择清单。

如何使用ASP.NET MVC创建下拉列表

假设Model.mes_id Contais选择的值,您可以做这样的事情

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

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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top