0

I want to use @Html.Partial("_partialView") to include a partial view on my page in MVC 3.

Both the page and the viewmodel have a viewmodel; thus, the following error is generated:

The model item passed into the dictionary is of type '[...]page', but this dictionary requires a model item of type '[...]partialview'.

How can I use the @Html.Partial() method while keeping the two viewmodels?

Helge
  • 823
  • 5
  • 13
  • 28

1 Answers1

1

You should use this overload that allows the model object to be passed to partial view

    public static MvcHtmlString Partial(
      this HtmlHelper htmlHelper,
      string partialViewName,
      Object model
    )

By the way, do you really need to call Partial? RenderPartial is better - it writes directly to the response stream(compared to partial that returns string), so reserves the memory. Partial views can be quite big, so there's memory overhead with Partial if you do not absolutely need it.

archil
  • 39,013
  • 7
  • 65
  • 82
  • So, you mean I can just the following instead? `@Html.RenderPartial("_partialView");` – Helge Sep 01 '11 at 10:46
  • No, RenderPartial has got same overload that allows model to be passed. You should pass model anyways. I meant memory overhead difference between RenderPartial and Partial - first writing directly to the result stream, and last returning string that you've got to write in result stream. – archil Sep 01 '11 at 10:53
  • @archil regarding your performance assertions, even "writing to the response stream" is actually buffered into a StringBuilder, so it really shouldn't make that much of a difference. I'd write the cleanest code and focus on perf optimization only if there's a measureable negative impact. – marcind Sep 01 '11 at 14:00
  • @marcind I got interested by your comment and explored source codes. As it seems, you are completely right. The only tiny bit of memory difference may be that that string buffer when using RenderPartial will be automatically GC-ed, whereas result string returned from Partial may stay in the memory longer. By the way, RenderPartial is cleaner than Partial IMHO, as it does not explicitely need second step of writing to result stream. – archil Sep 02 '11 at 10:18
  • 1
    @archil In Razor I think actually `Partial()` is cleaner, e.g. `@Partial("Foo")` vs `@{ RenderPartial("Foo"); }`. – marcind Sep 02 '11 at 17:20