Html.Partial not rendering partial view

假装没事ソ 提交于 2019-12-05 13:13:45
Ehsan Sajjad

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/

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!