ASP.NET MVC Razor Syntax Error

how can I translate this code into razor syntax:

<% for (int i = 0; i < items.Length; i++)  %>
<%{
    if (i % 3 == 0)
    { %>
      <tr>
 <% } %>
    <td><a href="<%: url[i] %>"><%: title[i] %></a></td>           
 <% if (i % 3 == 2)
    { %>
      </tr>        
 <% } %>
<%} %>

I try, but do not have time:

@for (int i = 0; i < items.Length; i++) 
{
    if (i % 3 == 0) 
    { 
        <tr>
    }
    <td><a href="@(url[i])">@(title[i])</a></td>
    if (i % 3 == 2) 
    {
        </tr>
    }
}
+3
source share
4 answers

I have a solution:

    @for (int i = 0; i < items.Length; i++)
{
    if (i % 3 == 0)
    {
@:<tr>
    }
    <td><a href="@url[i]"><img height="@(48 * Scale.Value)" width="@(48 * Scale.Value)" src="/i@(Scale.Value)/@(items[i]).png"/><span>@text[i]</span></a></td>
    if (i % 3 == 2)
    {
@:</tr>
    }
}

here i ntroduction for razor syntax but:

Use the @: operator or the <text>element. @: Outputs a single line of content containing plain text or unsurpassed HTML tags; The element encloses several lines of output. These options are useful when you do not want to display the HTML element as part of the output.

I don’t know why <text>it works now. perhaps because the razor is still RC and not a release

+5
source

, 3.

@for(int i=0 ; i < items.Length ; ) {
    <tr>
        @for(int maxInRow = i+3 ; i < items.Length && i<maxInRow ; i++) {
            <td><a href="@url[i]">@title[i]</a></td>
        }
    </tr>
}
+4

<text> IHtmlString :

@for (int i = 0; i < items.Length; i++) 
{
    if (i % 3 == 0) 
    { 
        <text><tr></text>
    }
    <td><a href="@(url[i])">@(title[i])</a></td>
    if (i % 3 == 2) 
    {
        @MvcHtmlString.Create("</tr>")        
    }
}

- :

, :

@functions {
    public IHtmlString conditionalTag(bool condition, string tag, Func<object, HelperResult> template) {
        var startTag = condition ? string.Format("<{0}>", tag) : "";
        var endTag = condition ? string.Format("</{0}>", tag) : "";
        return new HtmlString(string.Format("{0}{1}{2}", startTag, template(null).ToString(), endTag));
    }
}

:

@for (int i = 0; i < items.Length; i++) 
{
    @conditionalTag(i % 3 == 0, "tr", @<text>
       <td><a href="@(url[i])">@(title[i])</a></td>
    </text>)
 }
+1

Razor, , , , . - . Linq Where. :

@for (int i = 1; i < items.Length; i+=3) 
{ 
<tr> 
    <td><a href="@url[i]"><img height="@(48 * Scale.Value)" width="@(48 * Scale.Value)" src="/i@(Scale.Value)/@(items[i]).png"/><span>@text[i]</span></a></td> 
</tr> 
} 

, , , .

0

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


All Articles