开发者

Iteration in Razor vs ASPX

开发者 https://www.devze.com 2023-03-23 21:22 出处:网络
Is it possible to create a single line iteration command in my non-razor MVC view that does the following:

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消