如何获取某个下拉列表的选定值,在循环中调用多次?

发布于 2024-10-22 05:43:52 字数 531 浏览 5 评论 0原文

在我的应用程序中,我需要显示一些链接到显示行的下拉列表。

<table>
        <% foreach (var item in Model) { %>
                <td>
                <%= Html.Encode(item.COMPETENCE_LIBELLE) %>
            </td>
            <td>
                <%= Html.DropDownListFor(item.FK_NIVEAU_ID, (SelectList)ViewData["FK_Niveau"]%>
            </td>
<% } %>
</table>

但是,我不知道为什么,我的 ddlist 的选定值从未显示...

显示单个 ddlist 的选定值没有任何问题,但是当它循环时...我不知道该怎么办。 ..

有什么想法吗?

On my application, I need to display some dropdownlist, linked to a display line.

<table>
        <% foreach (var item in Model) { %>
                <td>
                <%= Html.Encode(item.COMPETENCE_LIBELLE) %>
            </td>
            <td>
                <%= Html.DropDownListFor(item.FK_NIVEAU_ID, (SelectList)ViewData["FK_Niveau"]%>
            </td>
<% } %>
</table>

However, I dont know why, the selected value of my ddlist is never displayed...

I havent got any problem to display the selected value of a single ddlist, but when its on a loop... I dont know what to do...

Any idea ?

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

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

发布评论

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

评论(1

2024-10-29 05:43:52

如果您需要显示多个下拉列表,那么您可能需要调整视图模型。让我们举个例子:

public class ItemsViewModel
{
    public string Label { get; set; }
    public string SelectedId { get; set; }
    public IEnumerable<SelectListItem> Values { get; set; }
}

public class MyViewModel
{
    public IEnumerable<ItemsViewModel> Items { get; set; }
}

然后有一个控制器操作来填充此视图模型:

public ActionResult Index() 
{
    var model = new MyViewModel
    {
        Items = new[]
        {
            // TODO: fetch from your repository
            new ItemsViewModel 
            { 
                Label = "label 1",
                Values = new[] 
                { 
                    new SelectListItem { Value = "1", Text = "item 1" },
                    new SelectListItem { Value = "2", Text = "item 2" }
                }
            },
            new ItemsViewModel 
            { 
                Label = "label 2",
                // Automatically preselect the second item in the ddl
                SelectedId = "B",
                Values = new[] 
                { 
                    new SelectListItem { Value = "A", Text = "foo1" },
                    new SelectListItem { Value = "B", Text = "bar" }
                }
            },
        }
    }
    return View(model);
}

然后有一个相应的强类型视图:

<table>
    <thead> 
        <tr>
            <th>Label</th>
            <th>Values</th>
        </tr>
    </thead>
    <tbody>
        <%= Html.EditorFor(x => x.Items)
    </tbody>
</table>

并在相应的编辑器模板中(~/Views/Shared/EditorTemplates/ItemsViewModel.ascx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.ItemsViewModel>" 
%>
<tr>
    <td>
        <%: Model.Label %>
    </td>
    <td>
        <%= Html.DropDownListFor(
            x => x.SelectedId, 
            new SelectList(Model.Items, "Values", "Text")
        ) %>
    </td>
</tr>

If you need to display multiple dropdown lists then you might need to adapt your view model. So let's take an example:

public class ItemsViewModel
{
    public string Label { get; set; }
    public string SelectedId { get; set; }
    public IEnumerable<SelectListItem> Values { get; set; }
}

public class MyViewModel
{
    public IEnumerable<ItemsViewModel> Items { get; set; }
}

then have a controller action which will populate this view model:

public ActionResult Index() 
{
    var model = new MyViewModel
    {
        Items = new[]
        {
            // TODO: fetch from your repository
            new ItemsViewModel 
            { 
                Label = "label 1",
                Values = new[] 
                { 
                    new SelectListItem { Value = "1", Text = "item 1" },
                    new SelectListItem { Value = "2", Text = "item 2" }
                }
            },
            new ItemsViewModel 
            { 
                Label = "label 2",
                // Automatically preselect the second item in the ddl
                SelectedId = "B",
                Values = new[] 
                { 
                    new SelectListItem { Value = "A", Text = "foo1" },
                    new SelectListItem { Value = "B", Text = "bar" }
                }
            },
        }
    }
    return View(model);
}

and then have a corresponding strongly typed view:

<table>
    <thead> 
        <tr>
            <th>Label</th>
            <th>Values</th>
        </tr>
    </thead>
    <tbody>
        <%= Html.EditorFor(x => x.Items)
    </tbody>
</table>

and in the corresponding editor template (~/Views/Shared/EditorTemplates/ItemsViewModel.ascx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.ItemsViewModel>" 
%>
<tr>
    <td>
        <%: Model.Label %>
    </td>
    <td>
        <%= Html.DropDownListFor(
            x => x.SelectedId, 
            new SelectList(Model.Items, "Values", "Text")
        ) %>
    </td>
</tr>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文