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?
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/EditorTemplatesor~/Views/Shared/EditorTemplatesfolder. 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.ascxand obviously strongly typed toAdjustmentModel. This template will then be automatically called for each element.