MVC3 Razor syntax. How do I convert this ASP.NET s

2019-08-15 07:40发布

问题:

Can anyone convert this ASP.NET syntax to RAZOR syntax? I can not convert it "one to one" to Razor syntax.

<% Themes.ThemesAvailable().ForEach(i => 
   {
       if (i.Equals(Themes.ThemeToUse))
       {%>
         <a href="" id="A1" style="font-size:x-large;color:Red"><%:i%></a>
        <%}
       else
       {%>
        <a href="" style="color:Blue" id="ChangeThemeTo_<%:i%>"><%:i%></a>
       <%} %>
    <br />
<% });%>

The following does not work (complains about CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement)

@Themes.ThemesAvailable().ForEach(i => {
       if (i.Equals(Themes.ThemeToUse)){
         @:<a href="" id="A1" style="font-size:x-large;color:Red"> + @i + </a>
       ;}else{
         @:<a href="" style="color:Blue" id='ChangeThemeTo_@i'>@i</a>
       ;}   })

and this does not work (suddenly it expects '}' at top of the page line1 col1)

@Themes.ThemesAvailable().ForEach(i => 
   {
       if (i.Equals(Themes.ThemeToUse)){
         @<text><a href="" id="A1" style="font-size:x-large;color:Red"> + @i + </a></text>
        ;} else {
         @<text><a href="" style="color:Blue" id='ChangeThemeTo_@i'>@i</a></text>
       ;}
})

Seems The @i within id='ChangeThemeTo_@i' stops the end text-tag from working. If I remove the '@' the element works. But get the same error as the first conversion try (CS0201).

Removing the usage of lambda, this works, but ONLY if I remove the '@' from id='ChangeThemeTo_i'

        @foreach (var i in Themes.ThemesAvailable()){
            if (i.Equals(Themes.ThemeToUse)){
<a href="" id="A1" style="font-size:x-large;color:Red"> + @i + </a> } else {
<a href="" style="color:Blue" id='ChangeThemeTo_i'>@i</a> } }

回答1:

For one thing I think your over-use of the @ symbol is giving you issues. Try:

@foreach(var i in Themes.ThemesAvailable()) 
{
    if (i.Equals(Themes.ThemeToUse))
    {
        <text><a href="" id="A1" style="font-size:x-large;color:Red"> + @i + </a></text>
    } 
    else 
    {
       <text><a href="" style="color:Blue" id='ChangeThemeTo_@i'>@i</a></text>
    }
}

Edit

For your latest problem where it's not writing out the id attributes properly, try using string.Format instead:

<a href="" style="color:Blue" id="@(string.Format("ChangeThemeTo_{0}", i))">@i</a>

Or simply concatenate them:

<a href="" style="color:Blue" id="@("ChangeThemeTo_" + i)">@i</a>


回答2:

So apparently you need text before the code to make the markup parser work

@foreach(var theme in Themes.ThemesAvailable()) {
    if (theme.Equals(Themes.ThemeToUse))
        <a href="" id="a1" style="font-size:x-large;color:Red">@theme</a>
    else
        <a href="" style="color:Blue" id="ChangeThemeTo_@(theme)">@theme</a>
}

The above is all that is necessary for your code to work. The reason you don't need to use the text tag is because text is just a mockup html tag that looks like html so that it forces the parser to go into html mode. Since a real html tag does the same thing you don't need to use text. The reason you need to use the @(theme) in your id tag for the second anchor tag is probably because it looks similar to an email address and the parser is told to ignore anything that looks like an email address. You can verify this by trying ChangeThemeTo_ @theme with a space between and you'll see that it works. the @() is there to force the compiler to recognize what is between the () as code. I hope this helps your understanding of the parser a bit more.