C# MVC Razor for 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11183096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC Razor for loop
提问by user349456
I have this code (nested inside a form post) but am continually getting the error that it's missing the closing }
我有这个代码(嵌套在表单帖子中)但我不断收到错误,它缺少结束}
@for(int i=0;i< itemsCount; i++){
<input type="hidden" @string.Format("name= item_name_{0} value= {1}",i,items[i].Description) >
<input type="hidden" @string.Format("name= item_name_{0} value= {1}",i,items[i].UnitPrice.ToString("c"))>
}
I've been staring at it long enough...can anyone help?
我已经盯着它看够久了……有人可以帮忙吗?
回答by Cheng Chen
Try put @:before your html code like this:
尝试@:像这样在你的 html 代码之前放置:
@for(int i=0;i< itemsCount; i++)
{
@: html code here
}
Alternatives:
1. wrap your html code with <text></text>2. use HtmlHelperto generate the html code
替代方案: 1. 用<text></text>2.包装您的 html 代码HtmlHelper以生成 html 代码
回答by Kane
Or you can use the Html.Rawhelper
或者你可以使用Html.Raw助手
@for(int i=0; i < itemsCount; i++)
{
<input type="hidden" @Html.Raw(string.Format("name= item_name_{0} value= {1}",i,items[i].Description)) />
<input type="hidden" @Html.Raw(@string.Format("name= item_name_{0} value= {1}",i,items[i].UnitPrice.ToString("c"))) />
}
回答by Meligy
try:
尝试:
@for (int i = 0; i < itemsCount; i++) {
<input type="hidden" name="@("item_name_" + i)" value="@items[i].Description" />
<input type="hidden" name="@("item_name_" + i)" value="@(items[i].UnitPrice.ToString("c"))" />
}
Note the changes / notes in prashanth's another as well.
请注意 prashanth 的另一个中的更改/注释。
回答by Prashanth Thurairatnam
Easiest way is to make use of HTML Helpers. Code will be clean as well (your name format for Description and UnitPrice seems to follow the same format; you may want to change it)
最简单的方法是使用 HTML Helpers。代码也会很干净(您的 Description 和 UnitPrice 的名称格式似乎遵循相同的格式;您可能想要更改它)
@for (int i = 0; i < itemsCount; i++)
{
@Html.Hidden(string.Concat("?tem_name_", i), items[i].Description)
@Html.Hidden(string.Concat("?tem_name_", i), items[i].UnitPrice.ToString("c"))
}
回答by alfdev
Try to enclose your for loop body between text tag.
尝试将您的 for 循环正文括在文本标记之间。
http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx
http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx
回答by Akshita
you may note that for writing a code block you can write in two ways
您可能会注意到,对于编写代码块,您可以通过两种方式编写
- For Only a line of Block ,just like you have written in your code and this encloses just the line that contains the preceding @
- For Code Block using @{... } ,this gives you freedon to use Code without preceding @ except within HTML expressions .for any html/text you must precede it with @: you want to print as is,otherwise Razor will try to interpret it as code(Since @: defines content as literal for every code expression under @: you must use @ again for code)
- 对于 Only a line of Block ,就像您在代码中编写的一样,这仅包含包含前面的 @ 的行
- 对于使用 @{...} 的代码块,这使您可以自由地使用代码而不使用 @ 前面的代码,除了在 HTML 表达式中。对于任何 html/文本,您必须在它前面加上 @:您想按原样打印,否则 Razor 将尝试将其解释为代码(因为@:将@下的每个代码表达式的内容定义为文字:您必须再次对代码使用@)
In your case you can do as following
在您的情况下,您可以执行以下操作
@{
for(int i=0; i < itemsCount; i++)
{
@:<input type="hidden" @Html.Raw(string.Format("name= item_name_{0} value= {1}",i,items[i].Description)) />
@:<input type="hidden" @Html.Raw(@string.Format("name= item_name_{0} value= {1}",i,items[i].UnitPrice.ToString("c"))) />
}
}

