如何绑定 Ienumerable在 foreach 循环中?
我有一个与模型强绑定的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用编辑器模板。这样,您不需要编写任何循环并仍然保持强类型:
然后您定义一个编辑器模板,该模板将为集合中的每个元素自动呈现(
~/Views/Shared/EditorTemplates/AdjustmentModel.html)。 ascx
):遵守将编辑器模板放置在
~/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:
and then you define an editor template which will be automatically rendered for each element of your collection (
~/Views/Shared/EditorTemplates/AdjustmentModel.ascx
):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 haveIEnumerable<AdjustmentModel>
, the file must be calledAdjustmentModel.ascx
and obviously strongly typed toAdjustmentModel
. This template will then be automatically called for each element.您可以使用Html.TextBox
You could use Html.TextBox