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
Or you can use the Html.Raw
helper
@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"))) />
}
Try to enclose your for loop body between text tag.
http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx
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.
you may note that for writing a code block you can write in two ways
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"))) />
}
}
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)
@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"))
}
Try put @:
before your html code like this:
@for(int i=0;i< itemsCount; i++)
{
@: html code here
}
Alternatives:
1. wrap your html code with <text></text>
2. use HtmlHelper
to generate the html code