Razor is missing

I have some problems with the rather complicated Table and Razor tags.

I took "@". (ex before if, etc.).

I played for about 30 minutes and I can’t find a way to do this. I always get the error that / or similiar does not have a closing tag. I played with @: etc., but just can't get it.

If someone can help me, and if someone can give a decent explanation for the @: tag, I would really appreciate it.

<div> if (Model.dsInfoUser.Tables[0].Rows.Count != 0) { <table> for (int i = 0; i < Model.dsInfoUser.Tables[0].Rows.Count; i++) { if (i % 2 == 1) { <tr class="tableEven"> } else { <tr class="tableOdd"> } @*Picture*@ if (i == 0) { <td rowspan="@Model.dsInfoUser.Tables[0].Rows.Count" class="tblPicture"><img src="@Model.dsInfoUser.Tables[0].Rows[i][1]" /></td> } <td> @Model.dsInfoUser.Tables[0].Rows[i][0].ToString() </td> <td> @Model.dsInfoUser.Tables[0].Rows[i][1].ToString() </td> </tr> if (i == 5) { <tr> <td> <text>Member Of:</text> </td> <td> <table> for (int j = 0; j < Model.dsInfoUser.Tables[1].Rows.Count; j++) { if (j % 2 == 1) { <tr class="tableEven"> } else { <tr class="tableOdd"> } <td rowspan="3"> <div style="width: 400px; overflow-y: scroll"> </div> </td> </tr> </table> </td> </tr> } </table> } </div> 

For those who would like to know, here is the fixed version:

 <div> @if (Model.dsInfoUser.Tables[0].Rows.Count != 0) { <table> @for (int i = 0; i < Model.dsInfoUser.Tables[0].Rows.Count; i++) { <tr class="@(i % 2 == 1 ? "tableEven" : "tableOdd")"> @if (i == 0) { <td rowspan="@Model.dsInfoUser.Tables[0].Rows.Count" class="tblPicture"><img src="@Model.dsInfoUser.Tables[0].Rows[i][1]" /></td> } <td> @Model.dsInfoUser.Tables[0].Rows[i][0].ToString() </td> <td> @Model.dsInfoUser.Tables[0].Rows[i][1].ToString() </td> </tr> if (i == 5) { <tr> <td> <text>Member Of:</text> </td> <td> <table> @for (int j = 0; j < Model.dsInfoUser.Tables[1].Rows.Count; j++) { <tr class="@(i % 2 == 1 ? "tableEven" : "tableOdd")"> <td rowspan="3"> <div style="width: 400px; overflow-y: scroll"> </div> </td> </tr> } </table> </td> </tr> } } </table> } </div> 
+4
source share
2 answers

You cannot do that. The razor expects to be properly hierarchical. In particular, it is illegal:

 if(condition) { <foo> } else { <foo> } </foo> 

Although we both know that this is a well-formed <foo></foo> , the razor does not see it. He sees 2 unclosed <foo> and completely unconnected </foo> out of nowhere.

In your case, the way to do this is:

 <tr class="@(i % 2 == 1 ? "tableEven" : "tableOdd")"> <td>...</td> </tr> 
+10
source
 if (i % 2 == 1) { <tr class="tableEven"> } else { <tr class="tableOdd"> } 

probably this is something that you dislike.

you should be able to rewrite it as

  string className = i%2 == 1 ? "tableEven" : "tableOdd" <tr class="@className"> 

and make the parser happy

+1
source

All Articles