Razor Syntax Doesn't Work As I Expected

with some razor syntax issues

gives a Parsor error saying that there is no close character "}" in the foreach block

<ul> @{var client = "null";} @foreach (var instance in Model) { if (instance.tbl_Policy.tbl_Client.txt_clientName != client) { client = instance.tbl_Policy.tbl_Client.txt_clientName; </ul><h1>@client</h1> <ul> } <li> @instance.tbl_Policy.txt_policyNumber - Assigned to : @instance.aspnet_Membership.aspnet_User.UserName @instance.ATLCheckType.Question <button type="button" rel="<%:instance.ATLCheckInstanceId.ToString()%>">DelFiled</button> <button type="button" rel="<%:instance.ATLCheckInstanceId.ToString()%>">DelLineItem</button> </li> } </ul> 
+4
source share
3 answers

Razor cannot handle unbalanced HTML tags in code blocks.

Modify the if block to handle unbalanced tags in plain text:

  if (instance.tbl_Policy.tbl_Client.txt_clientName != client) { client = instance.tbl_Policy.tbl_Client.txt_clientName; @:</ul><h1>@client</h1> @:<ul> } 
+7
source

Code needs to be reorganized to properly support balanced tags

 @foreach (var groupedClient in Model.GroupBy(i => i.tbl_Policy.tbl_Client.txt_clientName)) { <ul> <h1>@groupedClient.Key</h1> foreach(var instance in groupedClient) { <li> @instance.tbl_Policy.txt_policyNumber - Assigned to : @instance.aspnet_Membership.aspnet_User.UserName @instance.ATLCheckType.Question <button type="button" rel="@instance.ATLCheckInstanceId.ToString()">DelFiled</button> <button type="button" rel="@instance.ATLCheckInstanceId.ToString()">DelLineItem</button> </li> } </ul> } 
+7
source

What with all the things <%: %> there? You need to use the @ syntax.

 <ul> @{var client = "null";} @foreach (var instance in Model) { if (instance.tbl_Policy.tbl_Client.txt_clientName != client) { client = instance.tbl_Policy.tbl_Client.txt_clientName; </ul><h1>@client</h1> <ul> } <li> @instance.tbl_Policy.txt_policyNumber - Assigned to : @instance.aspnet_Membership.aspnet_User.UserName @instance.ATLCheckType.Question <button type="button" rel="@instance.ATLCheckInstanceId.ToString()">DelFiled</button> <button type="button" rel="@instance.ATLCheckInstanceId.ToString()">DelLineItem</button> </li> } </ul> 
+2
source

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


All Articles