asp.net mvc 4 - Html.Partial not rendering partial view -
i have following code in view:
@if (sitesession.subpagehelper.displaytype == displaytype.list) { html.partial("_sublandingpage_list"); } else { html.partial("_sublandingpage_grid"); }
and within partials have foreach loop this:
@foreach (product product in sitesession.subpagehelper.pagedproducts) { html code here }
where pagedproducts
got doing .take()
on cached list of products
now above code doesn't display paged products if change partial include @ symbol remove semi-colon:
@html.partial("_sublandingpage_grid")
it display products properly. can tell me difference between 2 version took me ages figure out why products weren't displaying
it razor syntax tell starting write c# code, if don't put @ considered plain text need put @ sign before writing c# code in view , in html helper method don't need put semicolon in front of razor syntax write helpers way.
for example:
@html.labelfor(x=>m.someproperty) // here @ telling writing c# statement
when write:
@if (sitesession.subpagehelper.displaytype == displaytype.list) { html.partial("_sublandingpage_list"); // wrong syntax } else { html.partial("_sublandingpage_grid"); }
the right way tell razor html helper , 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 understanding razor:
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 use renderpartial work in 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
, html.renderpartial
, visist these links:
html.partial vs html.renderpartial & html.action vs html.renderaction
Comments
Post a Comment