在 ASP.NET MVC 中将所选选项指定到下拉列表的最简单方法

发布于 2024-08-29 06:52:17 字数 640 浏览 4 评论 0原文

我的模型中有一个选项列表 (IEnumerable),我想在视图中的多个下拉列表中使用它。但每个下拉菜单都可以有不同的选择选项。

如果使用 Html.DropDownList 帮助器,是否有一种简单的方法可以简单地指定应选择哪个?

此时,我能看到的唯一方法是自己生成 html 并循环浏览选项列表,如下所示:

<% for(int i=0; i<10; i++) { %>
    <select name="myDropDown<%= i %>">
        <% foreach(var option in Model.Options) { %>
        <option value="<%= Html.Encode(option.optValue) %>" <%if(ShouldBeSelected(i)) {%> selected="selected"<% } %>><%= Html.Encode(option.optText) %></option>
        <% } %>
    </select>
<% } %>

I have a list of options (IEnumerable< SelectListItem >) in my model that I want to use in multiple dropdowns in my view. But each of these dropdowns could have a different selected option.

Is there an easy way to simply specfiy which should be selected if using the Html.DropDownList helper?

At this point, the only way I can see is to generate the html myself and loop through the list of options like so:

<% for(int i=0; i<10; i++) { %>
    <select name="myDropDown<%= i %>">
        <% foreach(var option in Model.Options) { %>
        <option value="<%= Html.Encode(option.optValue) %>" <%if(ShouldBeSelected(i)) {%> selected="selected"<% } %>><%= Html.Encode(option.optText) %></option>
        <% } %>
    </select>
<% } %>

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

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

发布评论

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

评论(3

箹锭⒈辈孓 2024-09-05 06:52:17

Html.DropDownList() 接受 SelectList 作为具有 SelectedValue 属性的参数。创建 SelectList 并将 SelectList 传递给 Html.DropDownList() 时指定所选项目。

Html.DropDownList() accepts a SelectList as a parameter which has a SelectedValue property. Specify the selected item when you create the SelectList and pass the SelectList to the Html.DropDownList().

猫性小仙女 2024-09-05 06:52:17

具有属性“Id”和“Name”的“Item”类
ViewModel 类有一个属性“SelectedItemId”
items = IEnumerable;

<%=Html.DropDownList("selectname", items.Select(i => new SelectListItem{ Text = i.Name, Value = i.Id.ToString(), Selected = Model.SelectedItemId.HasValue ? i.Id == Model.SelectedItemId.Value : false })) %>

class 'Item' with properties 'Id' and 'Name'
ViewModel class has a Property 'SelectedItemId'
items = IEnumerable<Item>

<%=Html.DropDownList("selectname", items.Select(i => new SelectListItem{ Text = i.Name, Value = i.Id.ToString(), Selected = Model.SelectedItemId.HasValue ? i.Id == Model.SelectedItemId.Value : false })) %>
想念有你 2024-09-05 06:52:17

下面的示例在页面上有 7 个下拉菜单,每个下拉菜单都有相同的 5 个选项。每个下拉菜单都可以选择不同的选项。

在我看来,我的表单中有以下代码:

<%= Html.DropDownListFor(m => m.ValueForList1, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList2, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList3, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList4, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList5, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList6, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList7, Model.AllItems)%>

然后我有一个如下所示的视图模型:

public class HomePageViewModel
{
    public List<SelectListItem> AllItems { get; set; }

    public string ValueForList1 { get; set; }
    public string ValueForList2 { get; set; }
    public string ValueForList3 { get; set; }
    public string ValueForList4 { get; set; }
    public string ValueForList5 { get; set; }
    public string ValueForList6 { get; set; }
    public string ValueForList7 { get; set; }

    public HomePageViewModel()
    {
        AllItems = new List<SelectListItem> 
        {
            new SelectListItem {Text = "First", Value = "First"},
            new SelectListItem {Text = "Second", Value = "Second"},
            new SelectListItem {Text = "Third", Value = "Third"},
            new SelectListItem {Text = "Fourth", Value = "Fourth"},
            new SelectListItem {Text = "Fifth", Value = "Fifth"},
        };
    }
}

现在在您的控制器方法中,声明如下:

        public ActionResult Submit(HomePageViewModel viewModel)

viewModel.ValueForList1 的值将设置为所选值。

当然,我建议使用数据库中的某种枚举或 ID 作为您的值。

Here's an example that has 7 drop downs on the page, each with the same 5 options. Each drop down can have a different option selected.

In my view, I have the following code inside my form:

<%= Html.DropDownListFor(m => m.ValueForList1, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList2, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList3, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList4, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList5, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList6, Model.AllItems)%>
<%= Html.DropDownListFor(m => m.ValueForList7, Model.AllItems)%>

Then I have a viewmodel like this:

public class HomePageViewModel
{
    public List<SelectListItem> AllItems { get; set; }

    public string ValueForList1 { get; set; }
    public string ValueForList2 { get; set; }
    public string ValueForList3 { get; set; }
    public string ValueForList4 { get; set; }
    public string ValueForList5 { get; set; }
    public string ValueForList6 { get; set; }
    public string ValueForList7 { get; set; }

    public HomePageViewModel()
    {
        AllItems = new List<SelectListItem> 
        {
            new SelectListItem {Text = "First", Value = "First"},
            new SelectListItem {Text = "Second", Value = "Second"},
            new SelectListItem {Text = "Third", Value = "Third"},
            new SelectListItem {Text = "Fourth", Value = "Fourth"},
            new SelectListItem {Text = "Fifth", Value = "Fifth"},
        };
    }
}

Now in your controller method, declared like this:

        public ActionResult Submit(HomePageViewModel viewModel)

The value for viewModel.ValueForList1 will be set to the selected value.

Of course, I'd suggest using some kind of enum or ids from a database as your value.

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