MVC3剃刀语法。我怎么这个ASP.NET风格转换为剃刀。 (也有问题@引号内) [英] MVC3 Razor syntax. How do I convert this ASP.NET style to Razor. (also issues with @ within quotes)
问题描述
任何人都可以转换成这个ASP.NET语法剃刀语法?我不能转换为一对一剃刀语法。
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 />
<% });%>
以下不起作用(抱怨CS0201:只有分配,调用,递增,递减和新对象前pressions可以作为一个语句)
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>
;} })
和这不起作用(突然,预计}在页面一号线COL1的顶部)
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>
;}
})
似乎@i ID内='ChangeThemeTo_ @我是从工作停止结束文本标记。如果删除了@元素的作品。但得到了同样的错误作为第一次转换尝试(CS0201)。
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).
删除拉姆达的使用,这个工作,但只有当我从ID删除'@'='ChangeThemeTo_i
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>
}
}
推荐答案
有关一件事,我觉得你过度使用@符号的是给你的问题。尝试:
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>
}
}
修改
有关它不是写出来的ID属性正确,请尝试使用的String.Format,而不是最新的问题:
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>
这篇关于MVC3剃刀语法。我怎么这个ASP.NET风格转换为剃刀。 (也有问题@引号内)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!