在 Asp.Net MVC 视图中使用 dropdownlistfor 和 foreach 吗?

发布于 2024-10-25 19:10:23 字数 1275 浏览 1 评论 0原文

我有一个带有 foreach 循环的视图,用于模型的列表属性。现在,我希望能够让用户使用下拉列表设置列表中每个项目的值。但我不知道该怎么做。当它不在 foreach 循环中时,我使用过类似的方法:

@Html.DropDownListFor(model => model.Level, new SelectList(new[] { 1, 2, 3, 4, 5 }, Model.Level))

但是当我需要在循环中引用 item.Level 时,我该如何执行此操作?这是我的查看代码:

<div id="formDiv">
    @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" }))
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Ny arbetserfarenhet</legend>
            <table>
                <tr>
                    @*<th></th>*@
                    <th>
                        Program
                    </th>
                    <th>
                        Nivå
                    </th>
                </tr>
                @foreach (var item in Model) {
                    <tr>
                        <td>
                            @item.Program.Name
                        </td>
                        <td>

                            @item.Level
                        </td>
                    </tr>
}
            </table>
        </fieldset>
    }
</div>

I have a View with a foreach loop for a list property of the model. Now, I want to be able to let the user set the value of each of the items in the list using a dropdownlist. But I'm not sure how to do that. I've used something like this when it is not in a foreach loop:

@Html.DropDownListFor(model => model.Level, new SelectList(new[] { 1, 2, 3, 4, 5 }, Model.Level))

But how do I do this when I need to reference item.Level in the loop instead? Here's my View code:

<div id="formDiv">
    @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" }))
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Ny arbetserfarenhet</legend>
            <table>
                <tr>
                    @*<th></th>*@
                    <th>
                        Program
                    </th>
                    <th>
                        Nivå
                    </th>
                </tr>
                @foreach (var item in Model) {
                    <tr>
                        <td>
                            @item.Program.Name
                        </td>
                        <td>

                            @item.Level
                        </td>
                    </tr>
}
            </table>
        </fieldset>
    }
</div>

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

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

发布评论

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

评论(4

叹倦 2024-11-01 19:10:23

我有一个带有 foreach 循环的视图,用于模式的列表属性

我建议您避免在视图中编写循环,以支持编辑器模板。所以:

@model IEnumerable<AppName.Models.ModelName>
<div id="formDiv">
    @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" }))
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Ny arbetserfarenhet</legend>
            <table>
                <tr>
                    <th>
                        Program
                    </th>
                    <th>
                        Nivå
                    </th>
                </tr>
                @Html.EditorForModel()
            </table>
        </fieldset>
    }
</div>

在相应的编辑器模板(~/Views/Shared/EditorTemplate/ModelName.cshtml)中:

@model AppName.Models.ModelName
<tr>
    <td>@Model.Program.Name</td>
    <td>
        @Html.DropDownListFor(
            model => model.Level, 
            new SelectList(
                Enumerable.Range(1, 5).Select(x => new { Value = x, Text = x }), 
                "Value", 
                "Text"
            )
        )
    </td>
</tr>

因此将为模型中的每个元素呈现编辑器模板(这是某种类型的集合) )。重要的是编辑器模板必须位于 ~/Views/Shared/EditorTemplates 中并命名为 XXX.cshtml,其中 XXX 是类型主视图模型集合中使用的名称。

I have a View with a foreach loop for a list property of the mode

I would recommend you to avoid writing loops in your views in favor of editor templates. So:

@model IEnumerable<AppName.Models.ModelName>
<div id="formDiv">
    @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" }))
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Ny arbetserfarenhet</legend>
            <table>
                <tr>
                    <th>
                        Program
                    </th>
                    <th>
                        Nivå
                    </th>
                </tr>
                @Html.EditorForModel()
            </table>
        </fieldset>
    }
</div>

and in the corresponding editor template (~/Views/Shared/EditorTemplate/ModelName.cshtml):

@model AppName.Models.ModelName
<tr>
    <td>@Model.Program.Name</td>
    <td>
        @Html.DropDownListFor(
            model => model.Level, 
            new SelectList(
                Enumerable.Range(1, 5).Select(x => new { Value = x, Text = x }), 
                "Value", 
                "Text"
            )
        )
    </td>
</tr>

So the editor template will be rendered for each element in your model (which is a collection of some type). The important part is that the editor template must be located in ~/Views/Shared/EditorTemplates and named XXX.cshtml where XXX is the type name used in your main view model collection.

离线来电— 2024-11-01 19:10:23

你有没有尝试过:

@Html.DropDownListFor(m => item.Level, new SelectList(new[] { 1, 2, 3, 4, 5 }, item.Level))

Have you tried:

@Html.DropDownListFor(m => item.Level, new SelectList(new[] { 1, 2, 3, 4, 5 }, item.Level))
贪了杯 2024-11-01 19:10:23

使用这个语法:

  @Html.DropDownListFor(model => model.Level, new SelectList(Model.level as System.Collections.IEnumerable, "VALUE_FIELD", "TEXT_FIELD", YOUR PROPERTY NAME)

use this syntax:

  @Html.DropDownListFor(model => model.Level, new SelectList(Model.level as System.Collections.IEnumerable, "VALUE_FIELD", "TEXT_FIELD", YOUR PROPERTY NAME)
爱的那么颓废 2024-11-01 19:10:23

MVC 将创建循环。只需使用编辑器模板,特殊文件夹中的部分视图,其余的就像魔术​​一样。

编辑器模板

@model Models.AdditionalAccountingLine
@Html.HiddenFor(m => m.IsRequired)
@Html.DropDownListFor(m => m.FieldValue, new SelectList(@Model.FieldValueOptions, "Key", "Value"), "")

查看

@Html.EditorFor(m => m.AdditionalAccountingLines);

MVC will create the loop. Just use an editor template, partial view in special folder, and the rest works like magic.

Editor Template

@model Models.AdditionalAccountingLine
@Html.HiddenFor(m => m.IsRequired)
@Html.DropDownListFor(m => m.FieldValue, new SelectList(@Model.FieldValueOptions, "Key", "Value"), "")

View

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