Iteration in Razor vs ASPX
Is it possible to create a single line iteration command in my non-razor MVC view that does the following:
@Model.Toys.Each(@<input name="Make@{@开发者_运维技巧(item.Index + 1)}" type="hidden" value="@item.Item.Make" />)
Even if something like this was possible I wold advice you against. It's ugly. You may take a look at templated Razor delegates instead.
Or a simple loop:
@for (var i = 0; i < Model.Toys.Length; i++) {
<input name="Make@(i)" type="hidden" value="@Model.Toys[i].Make" />
}
or an editor template:
@Html.EditorFor(x => x.Toys)
UPDATE:
It seems that you want to rewrite this code for WebForms. So:
<% for (var i = 0; i < Model.Toys.Length; i++) { %>
<input name="Make<%= i %>" type="hidden" value="<%= Model.Toys[i].Make %>" />
<% } %>
Try it: Templated Razor Delegates
http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx
His solution is exactly for your issue :D
You can also simply create an EditorTemplate to handle objects of type Toy (naming conventions handle this, so you could simply call it toy.cshtml if that's it's strong type) and then in your view you can simply have @Html.EditorFor(x=>x.Toys)
精彩评论