如何绑定 Ienumerable在 foreach 循环中?

发布于 2025-01-08 06:04:48 字数 851 浏览 0 评论 0原文

我有一个与模型强绑定的 MVC2 视图。

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Collections.Generic.IEnumerable<AdjustmentModel>>" %>

在我的观点中。

         foreach (var adjustment in Model)
           {%>
         <tr row="<%: row %>">
            <td class="nonSelectable" column="0">
                <div>
                    ...I want to put a textbox here that has name="someId" 
                   and val="someVal" but how?
                </div>
            </td>

            }%>

该模型是

class AdjustmentModel
{
    public int ID;
    public int amount;
}

我无法计算出传递给 Html.TextBoxFor() 的委托,因为当我传递模型时,智能感知将无法工作,因为我传递的是 Ienumerable,所以我如何使用该模型?

I have an MVC2 view which is strongly bound to a model.

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Collections.Generic.IEnumerable<AdjustmentModel>>" %>

And in the View I have.

         foreach (var adjustment in Model)
           {%>
         <tr row="<%: row %>">
            <td class="nonSelectable" column="0">
                <div>
                    ...I want to put a textbox here that has name="someId" 
                   and val="someVal" but how?
                </div>
            </td>

            }%>

The model is

class AdjustmentModel
{
    public int ID;
    public int amount;
}

I just can't figure the delegate that I pass to Html.TextBoxFor() because when I pass a model the intellisense won't work as I'm passing in an Ienumerable so how do I use the model?

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

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

发布评论

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

评论(2

话少情深 2025-01-15 06:04:48

我建议您使用编辑器模板。这样,您不需要编写任何循环并仍然保持强类型:

<table>
    <%= Html.EditorForModel() %>
</table>

然后您定义一个编辑器模板,该模板将为集合中的每个元素自动呈现(~/Views/Shared/EditorTemplates/AdjustmentModel.html)。 ascx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AdjustmentModel>" 
%>
<tr>
    <td class="nonSelectable" column="0">
        <div>
            <%= Html.HiddenFor(x => x.ID) %>
            <%= Html.EditorFor(x => x.amount) %>
        </div>
    </td>
</tr>

遵守将编辑器模板放置在 ~/Views/CurrentController/EditorTemplates 中的约定非常重要,或者~/Views/Shared/EditorTemplates 文件夹。模板的名称必须是集合中使用的类型。例如,如果您有 IEnumerable,则该文件必须名为 AdjustmentModel.ascx,并且显然强类型为 AdjustmentModel。然后,每个元素都会自动调用该模板。

I would recommend you using editor templates. This way you don't need to write any loops and still keep the strong typing:

<table>
    <%= Html.EditorForModel() %>
</table>

and then you define an editor template which will be automatically rendered for each element of your collection (~/Views/Shared/EditorTemplates/AdjustmentModel.ascx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<AdjustmentModel>" 
%>
<tr>
    <td class="nonSelectable" column="0">
        <div>
            <%= Html.HiddenFor(x => x.ID) %>
            <%= Html.EditorFor(x => x.amount) %>
        </div>
    </td>
</tr>

It is important to respect the convention which is to locate your editor template inside the ~/Views/CurrentController/EditorTemplates or ~/Views/Shared/EditorTemplates folder. The name of the template must be the type used in the collection. So for example if you have IEnumerable<AdjustmentModel>, the file must be called AdjustmentModel.ascx and obviously strongly typed to AdjustmentModel. This template will then be automatically called for each element.

蘑菇王子 2025-01-15 06:04:48

您可以使用Html.TextBox

<%= Html.TextBox("SomeId", "SomeVal") %>

You could use Html.TextBox

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