Josh
Josh

Reputation: 1970

CS1525 Invalid expression term '>' in razor view page ASP.NET-core 2.2

I have the following code in the razor page view of my ASP.NET-Core 2.2 MVC application

<p> 
   @{ 
      if(course.Description.Length > 100)
      {
       @course.Description.Substring(0, 100)  @:"..."
      }
      else
      {
      @course.Description;
      }
    }
 </p>

but it gives me he following error

Error CS1525 Invalid expression term '>'

I dont know how to resolve this. Please help me if you can.

Thank you

Upvotes: 1

Views: 3103

Answers (3)

Hamit YILDIRIM
Hamit YILDIRIM

Reputation: 4549

its interesting with this @ compiler couldnt decide when used abnormally, for example if i use it like this

@functions{
    public IHtmlContent RenderSubButton(ButtonModel button)
    {
        var @appIcn = "blabla";

        return @Html.Raw(@"<a href='@Url.Action(" + button.Action + ", " + button.Controller + ", " + button.RouteValues + ")' class='" + button.Class + "' " + button.Binding.ToDataAttributes() + ">"
           + button.Text
           + @appIcn + "</a>");
        }
}

appIcon will not work but if i use

@functions{
    public IHtmlContent RenderSubButton(ButtonModel button)
    {
        var @appIcn = "blabla";

        return @Html.Raw(@"<a href='@Url.Action(" + button.Action + ", " + button.Controller + ", " + button.RouteValues + ")' class='" + button.Class + "' " + button.Binding.ToDataAttributes() + ">"
           + button.Text
           + appIcn + "</a>");
        }
}

then it will work. Difference is appIcn has @ this on head on first, no need to it

Upvotes: 0

Versailles
Versailles

Reputation: 461

Your code is working fine for me.

Here's a more concise way of writing it, though. See if it somehow works for you:

<p>
    @if (course.Description.Length > 100)
    {
        @course.Description.Substring(0, 100) @:"..."
    }
    else
    {
        @course.Description
    }
</p>

If you're still having issues, something else could be wrong with the surrounding code in your Razor page, or your project isn't referencing the proper assemblies? Try running the same code inside a new, blank ASP.NET project.

Upvotes: 1

zing
zing

Reputation: 621

I think the "course" in your if statement is missing an '@'. Try:

<p> 
      @{ 
      if(@course.Description.Length > 100)
      {
       @course.Description.Substring(0, 100)  @:"..."
      }
      else
      {
      @course.Description;
      }
    }
 </p>

Upvotes: 1

Related Questions