75

I was trying to use the following statement:

@Html.Test<ISQL>().Nand()

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

Any official work around for it?

marcind
  • 52,944
  • 13
  • 125
  • 111
kidoman
  • 2,402
  • 5
  • 26
  • 35
  • ...are you missing something because you didn't put that in a code block? I don't see any less than sign... – Paul Nov 23 '10 at 06:05

5 Answers5

109

To use generic methods you need to escape the expression

@(Html.Test<ISQL>().Nand())
marcind
  • 52,944
  • 13
  • 125
  • 111
16

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<SelectListItem>)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<SelectListItem>)ViewBag.Options;    
}

Hope this helps to anyone

Juan Carrey
  • 696
  • 1
  • 6
  • 13
  • I got crazy too on this one! "The code block is missing a closing } character." – SandRock Apr 13 '14 at 13:32
  • 1
    Thanks. Really helped - a previous @ was causing issues on the next statement, not something you would point directly at. – Darius Oct 17 '14 at 13:08
6

I had a strange case when I had several assignments inside of one @{ ... } bock.

@{
    // ... other assignments (without error)
    List<string> stringList = new List<string>()  // ERROR MESSAGE HERE
}

What ever I did, there were always errors, like this:

Using the generic type 'List' requires one type arguments

The solution: I put the assignment line to a second @{ ... } bock.

@{
    // ... other assignments
}
@{
    List<string> stringList = new List<string>()  // WORKS
}
Beauty
  • 865
  • 11
  • 14
6

I appreciate that this 'answer' is somewhat late and the question has obviously been satisfactorily (and well) answered. However, for any future visitors to this page, if you're after a more complete reference guide there's the MS introduction to Razor syntax as well as Phil Haack's very useful Razor quick reference blog post.

Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
2

It's about the @ character, this example works before upgrade

@{
    string @class = ViewBag.@class;
    IDictionary<string, object> htmlAttributes = new Dictionary<string, object>();
}

After upgrade, it must be separated into two blocks

@{  string @class = ViewBag.@class; }
@{  IDictionary<string, object> attrs = new Dictionary<string, object>(); }
Serguei Fedorov
  • 7,763
  • 9
  • 63
  • 94
fred
  • 693
  • 1
  • 7
  • 19