CS1525 razor 视图页面 ASP.NET-core 2.2 中的无效表达式术语“>”

我的 ASP.NET-Core 2.2 MVC 应用程序的剃刀页面视图中有以下代码


<p> 

   @{ 

      if(course.Description.Length > 100)

      {

       @course.Description.Substring(0, 100)  @:"..."

      }

      else

      {

      @course.Description;

      }

    }

 </p>

但它给了我以下错误


错误 CS1525 表达式术语“>”无效


我不知道如何解决这个问题。如果可以,请你帮助我。


蝴蝶不菲
浏览 72回答 3
3回答

慕姐8265434

您的代码对我来说工作正常。不过,这里有一种更简洁的写法。看看它是否对你有用:<p>&nbsp; &nbsp; @if (course.Description.Length > 100)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; @course.Description.Substring(0, 100) @:"..."&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; @course.Description&nbsp; &nbsp; }</p>如果您仍然遇到问题,那么您的 Razor 页面中的周围代码可能有其他问题,或者您的项目没有引用正确的程序集?尝试在一个新的空白 ASP.NET 项目中运行相同的代码。

隔江千里

我认为您的 if 语句中的“课程”缺少“@”。尝试:<p>&nbsp;&nbsp; &nbsp; &nbsp; @{&nbsp;&nbsp; &nbsp; &nbsp; if(@course.Description.Length > 100)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;@course.Description.Substring(0, 100)&nbsp; @:"..."&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; @course.Description;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;</p>

弑天下

无法决定何时异常使用,例如,如果我这样使用它@functions{&nbsp; &nbsp; public IHtmlContent RenderSubButton(ButtonModel button)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var @appIcn = "blabla";&nbsp; &nbsp; &nbsp; &nbsp; return @Html.Raw(@"<a href='@Url.Action(" + button.Action + ", " + button.Controller + ", " + button.RouteValues + ")' class='" + button.Class + "' " + button.Binding.ToDataAttributes() + ">"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ button.Text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ @appIcn + "</a>");&nbsp; &nbsp; &nbsp; &nbsp; }}appIcon 将不起作用,但如果我使用@functions{&nbsp; &nbsp; public IHtmlContent RenderSubButton(ButtonModel button)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var @appIcn = "blabla";&nbsp; &nbsp; &nbsp; &nbsp; return @Html.Raw(@"<a href='@Url.Action(" + button.Action + ", " + button.Controller + ", " + button.RouteValues + ")' class='" + button.Class + "' " + button.Binding.ToDataAttributes() + ">"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ button.Text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ appIcn + "</a>");&nbsp; &nbsp; &nbsp; &nbsp; }}然后它会工作。区别在于 appIcn 前面有 @this,不需要它
打开App,查看更多内容
随时随地看视频慕课网APP