Reputation: 3930
For an ASP.NET, MVC application, when using grid
with Razor syntax, why do I need the @
rightbefore the line grid.GetHtml();
?
Example -
@{
var grid = new WebGrid(Model);
@grid.GetHtml();
}
Shouldn't surrounding grid.GetHtml();
with @{ }
be enough?
Thanks!
Upvotes: 1
Views: 812
Reputation: 12925
Although what did you did is fully supported in Razor view engine and sometimes can not be done differently it is better to be more specific:
@{
var grid = new WebGrid(Model);
}
@grid.GetHtml();
So to clearly separate the Code from the outputting. I think it make sense for better code/view readability.
Upvotes: 0
Reputation: 5989
Using @ is like printing/writing something on the page.
grid.GetHtml()
will generate the required string but this function doesn't have any idea about printing the generated html. to do this you have to write it like this
@grid.GetHtml()
There are some helpers where you don't required to put @ before the statement.
for example
Html.RenderPartial()
Because MVC writes the generated html to the response stream. Hence here this statement should get included in
@{ }
block in some cases it returns the HTML and we are expected to call print for it using "@"
Upvotes: 0
Reputation: 3506
With @{} you specify that some piece of code should be executed, like declaring the variable "grid".
Now for the second line, without the @, you are only left with a statement that equals a string, which is not even syntactically supported.
While using the @, that line translates to something like:
Response.Write(grid.GetHtml());
Upvotes: 1