8

I have the following code in a view:

@if (SiteSession.SubPageHelper.DisplayType == DisplayType.List)
{
    Html.Partial("_SubLandingPage_List");
}
else
{
    Html.Partial("_SubLandingPage_Grid");
}

and within the partials I just have a foreach loop like this:

@foreach (Product product in SiteSession.SubPageHelper.PagedProducts)
{
      some html code here
}

Where PagedProducts is got from doing a .Take() on a cached list of products

Now the above code doesn't display my paged products but if I change the partial to include the at symbol remove the semi-colon:

@Html.Partial("_SubLandingPage_Grid")

It will display the products properly. Can anyone tell me what the difference between the two version are as it took me ages to figure out why the products weren't displaying

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160

2 Answers2

20

It is actually razor syntax to tell that we are starting to write c# code, if your don't put @ it will be considered as plain text so you need to put @ sign before writing c# code in the view and in html helper method you don't need to put semicolon in front of then it is razor syntax to write helpers this way.

For Example:

@Html.LabelFor(x=>m.SomeProperty) // here @ is telling that we are writing c# statement

When you write:

@if (SiteSession.SubPageHelper.DisplayType == DisplayType.List)
{
    Html.Partial("_SubLandingPage_List"); // this is wrong syntax 
}
else
{
    Html.Partial("_SubLandingPage_Grid");
}

the right way is to tell that this is a razor html helper and c# statement:

@if (SiteSession.SubPageHelper.DisplayType == DisplayType.List)
{
    @Html.Partial("_SubLandingPage_List")
}
else
{
    @Html.Partial("_SubLandingPage_Grid")
}

you can see more on razor syntax HERE

Few more links which will help you understanding razor:

http://www.asp.net/web-pages/tutorials/basics/2-introduction-to-asp-net-web-programming-using-the-razor-syntax

http://weblogs.asp.net/scottgu/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax

http://weblogs.asp.net/scottgu/introducing-razor

UPDATE:

An alternative can be to use RenderPartial which will work in the if statement without putting @ sign:

@if (SiteSession.SubPageHelper.DisplayType == DisplayType.List)
    {
        Html.RenderPartial("_SubLandingPage_List");
    }
    else
    {
        Html.RenderPartial("_SubLandingPage_Grid");
    }

For understanding Difference between Html.Partial and Html.RenderPartial, visist these links:

Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction

http://dotnethelpers.wordpress.com/2013/06/18/difference-between-html-renderpartial-vs-html-partial-and-html-renderaction-vs-html-action-in-mvc/

http://www.em64t.net/2010/12/razor-html-renderpartial-vs-html-partial-html-renderaction-vs-html-action-what-one-should-use/

Community
  • 1
  • 1
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • 2
    But if it is considered plain text then why isn't that rendered to the screen instead of nothing at all. Also when I debugged and stepped through the code it still went into the partial but just didn't have anything in the products collection –  Jun 27 '14 at 09:57
  • razor syntax is to put ``@`` and semicolon is not needed at end of its statement, you should read about razor syntax to get your mind clear how to use it – Ehsan Sajjad Jun 27 '14 at 10:02
  • 1
    Ah okay, you were almost correct - it is still considered as c# instead of plain text as it is within the if statement being such you need to use `HTML.RenderPartial("_SubLandingPage_List");`. by putting the `@` in front in makes it come back to Razor in which case the `HTML.Partial` is enough to render it. If you change your answer to reflect this I will accept it –  Jun 27 '14 at 10:05
  • Html.Partial does not return you the partial view in main view?? – Ehsan Sajjad Jun 27 '14 at 10:16
1

Well simply the "@" symbol instructs Razor that it have to render something. The @ always expect something to print from the supplied statement. If you don't want to use this syntax you can still render the content with minor changes in your code in loop .. look at this.

@if (SiteSession.SubPageHelper.DisplayType == DisplayType.List)
{
    Html.RenderPartial("_SubLandingPage_List"); // this is wrong syntax 
}
else
{
    Html.RenderPartial("_SubLandingPage_Grid");
}

Html.RenderPartial will directly write it to stream and doesn't requires @. Even if you try to write something like following

@Html.RenderPartial("_SubLandingPage_Grid")

This will give you an error as @ expect something to return and Html.RenderPartial returns void

K D
  • 5,889
  • 1
  • 23
  • 35
  • The problem can be also solved by adding `@: @` before Html.Partial call – FLCL Jun 27 '14 at 10:17
  • Yeah the problem can be solved by adding @. but i am just explaining him the behavior of "@" :) and another solution for added knowledge .. because it is not a weird behavior of razor so far... Cheers ;) – K D Jun 27 '14 at 10:41