How to use generic syntax inside a Razor view file?

后端 未结 5 1625
遥遥无期
遥遥无期 2021-02-04 23:26

I was trying to use the following statement:

@Html.Test().Nand()

However, Razor is choking at the < before the ISQL.

Any

5条回答
  •  北荒
    北荒 (楼主)
    2021-02-04 23:37

    I just found this question when I was looking for this "same error" when upgrading mvc.

    I had :

    Does not work:

    @{ 
       ViewBag.Title = "Something " + @Model.Title;
       var something = (IEnumerable)ViewBag.Options;    
    }
    

    Apparently, the syntax went stricter, and as you are inside a @{} block, you should not add @ before Model.Title on the example. But the error on the code editor was pointing to the generic and it was getting me crazy.

    It works fine if there is no <> inside the code, but just removing the @ from Model.Title fix the issue.

    Works:

    @{ 
       ViewBag.Title = "Something " + Model.Title;
       var something = (IEnumerable)ViewBag.Options;    
    }
    

    Hope this helps to anyone

提交回复
热议问题