Razor renderpartial exception - expected '} "

In my code:

@foreach (var post in Model.Posts) { Html.RenderPartial("ShowPostPartial", post); } 

I have a use case on the RenderPartial line.

error CS1513:} expected.

What am I doing wrong?

+4
source share
5 answers

For completeness, here is another way to invoke this:

 @if(condition) { <input type="hidden" value="@value"> } 

The problem is that an unclosed element makes it not obvious enough that the content is an html block (but we do not always do xhtml, right?).

In this case you can use:

 @if(condition) { @:<input type="hidden" value="@value"> } 

or

 @if(condition) { <text><input type="hidden" value="@value"></text> } 
+2
source

This is basically the same answer that Mark Gravell gave, but I think this is a simple mistake, if you have a larger view: Check the html tags to see where they start and end, and razor syntax occurs between them, this is incorrect:

 @using (Html.BeginForm()) { <div class="divClass"> @Html.DisplayFor(c => c.SomeProperty) } </div> 

And it is right:

 @using (Html.BeginForm()) { <div class="divClass"> @Html.DisplayFor(c => c.SomeProperty) </div> } 

Again, almost the same as the previous post about an unclosed input element, but just beware, I placed the div incorrectly many times when changing the view.

+2
source

I have a problem with Razor. I'm not sure if this is a parser error or that, but the way I decided is to break it down:

 @using(Html.BeginForm()) { <h1>Example</h1> @foreach (var post in Model.Posts) { Html.RenderPartial("ShowPostPartial", post); } } 

in

 @{ Html.BeginForm(); } <h1>Example</h1> @foreach (var post in Model.Posts) { Html.RenderPartial("ShowPostPartial", post); } @{ Html.EndForm(); } 
0
source

MY is bad. I have an error in a partial view. I wrote "class" instead of "@class" in htmlAttributes.

0
source

The MVC4 Razor Parser is different from MVC3. Razor v3 has advanced parser capabilities and, on the other hand, rigorous analysis compared to MVC3.

-> Avoid using server blocks in views if there is no variable declaration section.

Not: \ n @{if(check){body}} Recommended: @if(check){body}

-> Avoid using @ when you are already in server scope.

Not: @if(@variable) Recommended: @if(variable)

Not: @{int a = @Model.Property } Recommended: @{int a = Model.Property }

Reference: https://code-examples.net/en/q/c3767f

0
source

Source: https://habr.com/ru/post/1344472/


All Articles