如何在asp.net mvc中使下拉列表显示选定的值?

发布于 2024-08-31 01:39:38 字数 1225 浏览 4 评论 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;
        }

I have an edit page with a Html.DropDownList in it....I cant show the dropdownlist value it always shows up with Select instead i want to make the dropdown show an item as selected based on a model value say Model.Mes_Id... Any suggestion how it can be done...

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

EDIT: It has the list items but i want to show a value selected in the edit view...

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

My repository method,

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

关于从前 2024-09-07 01:39:38

创建 IEnumerable 时设置所选项目。

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

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

Set the selected item when you create the IEnumerable<SelectListItem>.

Personally I would create a specialized viewmodel for the form but going by your code, do something like:

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

Then change your repository method to something like:

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,
Charles

衣神在巴黎 2024-09-07 01:39:38

看看这篇博客文章。

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

基本上,您需要转换您的 measurementTypes 列表/可枚举到 SelectListIEnumerable

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

Have a look at this blog entry.

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

Basically, you need to convert your mesurementTypes list/enumerable into a SelectList or IEnumerable<SelectListItem>.

I would recommend, if possible, upgrading to ASP.NET MVC2 and using Html.DropDownListFor()

很酷又爱笑 2024-09-07 01:39:38

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

如何使用 ASP.NET MVC 创建 DropDownList

You should be returning a SelectionList which can specify a selected item.

How to create a DropDownList with ASP.NET MVC

她比我温柔 2024-09-07 01:39:38

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

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

Assuming that Model.Mes_Id contais the selected value, you can do something like this

<%
    var Measurements = new SelectList((IEnumerable)ViewData["MeasurementType"], "Id", "Name", Model.Mes_Id);
    Response.Write(Html.DropDownList("measurement_type", Measurements, "Select"));
%>
像极了他 2024-09-07 01:39:38

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>

Html.DropDownListFor didn't work for me So I got the poppy like this

    (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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文