它实际上是 razor 语法,告诉我们我们开始编写 C# 代码,如果你不输入@它将被视为纯文本,因此您需要输入@在视图和 html 帮助器方法中编写 C# 代码之前进行签名,您不需要在前面放置分号,那么以这种方式编写帮助器是 razor 语法。
例如:
@Html.LabelFor(x=>m.SomeProperty) // here @ is telling that we are writing c# statement
当你写:
@if (SiteSession.SubPageHelper.DisplayType == DisplayType.List)
{
Html.Partial("_SubLandingPage_List"); // this is wrong syntax
}
else
{
Html.Partial("_SubLandingPage_Grid");
}
正确的方法是告诉这是一个 razor html 帮助程序和 c# 语句:
@if (SiteSession.SubPageHelper.DisplayType == DisplayType.List)
{
@Html.Partial("_SubLandingPage_List")
}
else
{
@Html.Partial("_SubLandingPage_Grid")
}
您可以查看有关 razor 语法的更多信息HERE http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/
更多链接将帮助您了解剃须刀:
http://www.asp.net/web-pages/tutorials/basics/2-introduction-to-asp-net-web-programming-using-the-razor-syntax http://www.asp.net/web-pages/tutorials/basics/2-introduction-to-asp-net-web-programming-using-the-razor-syntax
http://weblogs.asp.net/scottgu/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax http://weblogs.asp.net/scottgu/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax
http://weblogs.asp.net/scottgu/introducing-razor http://weblogs.asp.net/scottgu/introducing-razor
UPDATE:
另一种方法是使用 RenderPartial ,它将在 if 语句中工作,而无需放置@ sign:
@if (SiteSession.SubPageHelper.DisplayType == DisplayType.List)
{
Html.RenderPartial("_SubLandingPage_List");
}
else
{
Html.RenderPartial("_SubLandingPage_Grid");
}
为了理解之间的区别Html.Partial
and Html.RenderPartial
,访问这些链接:
Html.Partial 与 Html.RenderPartial 和 Html.Action 与 Html.RenderAction https://stackoverflow.com/questions/5248183/html-partial-vs-html-renderpartial-html-action-vs-html-renderaction
http://dotnethelpers.wordpress.com/2013/06/18/difference- Between-html-renderpartial-vs-html-partial-and-html-renderaction-vs-html-action-in-mvc/ http://dotnethelpers.wordpress.com/2013/06/18/difference-between-html-renderpartial-vs-html-partial-and-html-renderaction-vs-html-action-in-mvc/
http://www.em64t.net/2010/12/razor-html-renderpartial-vs-html-partial-html-renderaction-vs-html-action-what-one-should-use/ http://www.em64t.net/2010/12/razor-html-renderpartial-vs-html-partial-html-renderaction-vs-html-action-what-one-should-use/