2

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 Sharwe.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?

1 Answer 1

6

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") %>
4
  • Ah I see. That actually worked, but now the model type of the partial view is conflicting with the model type of the @RenderBody region. Is there anyway to solve this issue?
    – Kassem
    Mar 17, 2011 at 20:18
  • You can pass the correct model as second argument to the RenderPartial method. Mar 17, 2011 at 20:21
  • I tried to do that but I couldn't get it right. I still get the same error. Mind showing me an example please?
    – Kassem
    Mar 17, 2011 at 20:27
  • Follow-up: stackoverflow.com/questions/5345029/…
    – Kassem
    Mar 17, 2011 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.