Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Razor for loop

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?

like image 902
user349456 Avatar asked Jun 25 '12 03:06

user349456


People also ask

Can you use MVC with razor pages?

You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.

Which is better razor or MVC?

MVC works well with apps that have a lot of dynamic server views, single page apps, REST APIs, and AJAX calls. Razor Pages are perfect for simple pages that are read-only or do basic data input. MVC has been all the rage recently for web applications across most programming languages.

Why razor is used in MVC?

It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML. It's just that ASP.NET MVC has implemented a view engine that allows us to use Razor inside of an MVC application to produce HTML.

What is Cshtml?

What is a CSHTML file? A file with . cshtml extension is a C# HTML file that is used at server side by Razor Markup engine to render the webpage files to user's browser.


1 Answers

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

like image 169
Cheng Chen Avatar answered Sep 30 '22 02:09

Cheng Chen