0

I am using ASP.NET MVC 3. I have the following piece of code in my razor view:

@foreach(var c in Model.ChildCategories) {
     <div>@c.Name<br></div>
}

It works well. Why when I take away the innder div tags will it not work? This is what I tried:

@foreach(var c in Model.ChildCategories) {
     @c.Name<br>
}

I am not closing the break tag because I am using the strict doc type:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

The error that I get when I want to view the view is:

The foreach block is missing a closing "}" character.  Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.

I would prefer not to have the inner div tags because I don't want unnecessary tags in my markup.

I also have the following piece of code:

@if (Model.ParentCategory != null)
{
     @Model.ParentCategory.Name
}
else
{
     @:N/A
}

I want to display the name of the parent category, or just the plain text N/A. Am I doing it correctly? Is it better to have the curly brackets beneath the if or next to it like if (...) {?

2 Answers 2

1

Check out Phil Haack's post razor on syntax

@foreach(var c in Model.ChildCategories) 
{
  <text>
     @c.Name<br />
  </text>
}

but instead of using <br /> consider using <p>

@foreach(var c in Model.ChildCategories) 
{
  <p>
     @c.Name
  </p>
}

the last bit with @:N/A is correct.

Sign up to request clarification or add additional context in comments.

Comments

0

try this~

@foreach(var c in Model.ChildCategories) {
   <div>@{@c.Name}<br></div>
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.