开发者

RenderPartial Problem (While sending data to master page)

I am trying to send some data the master page. I am using Razor as my View Engine, and AutoMapper to map between Domain and View Models. Below is my code:

//the partial view action method:
public ActionResult RenderCategories()
{
    IEnumerable<TopLevelCategory> categories = _categoryService.GetTopLevelCategories();
    var viewModel = new MasterPageViewModel
                    {
                        Categories =
                            Mapper.Map
                            <IEnumerable<TopLevelCategory>, IEnumerable<ParentCategory>>(categories)
                    };

    return View(viewModel);
}

//the partial view file: (this is just a test for now)
@model S开发者_如何转开发harwe.MVC.ViewModels.MasterPageViewModel

@foreach (var item in Model.Categories) {
      @item.Name
}

public class MasterPageViewModel
{
    public IEnumerable<ParentCategory> Categories { get; set; }
}

I'm calling the partial view using @Html.RenderPartial("RenderCategories") from inside my _Layout.cshtml file. But I keep getting the following error:

Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments

What's going on?


The RenderPartial extension method writes directly to the output stream and has no return type (void). So here's the correct syntax to call it:

@{Html.RenderPartial("RenderCategories");}

or if you prefer you could use the Partial extension method:

@Html.Partial("RenderCategories")

Contrast those with their equivalents in the WebForms view engine:

<% Html.RenderPartial("RenderCategories"); %>
<%= Html.Partial("RenderCategories") %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜