1

I have a site using a single master.page. In the master.page I have a strongly typed user control (.ascx) called "ControlPanel". Its type is ControlPanelViewModel.

Here is the mater page - MasterFrame.master:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<body>
    <% Html.RenderPartial("ControlPanel"); %> <!-- Here I need to pass the ControlPanelViewModel -->

    <!-- main content area -->
</body>
</html>

Here is the custom control page - ControlPanel.ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ControlPanelViewModel>" %>
<!-- Some content which are using the ControlPanelViewModel -->

Now the question is how to send the correct ControlPanelViewModel to master page's custom control? What is the best approach? Is there some sort of good practice or design pattern for this case?

devfreak
  • 1,201
  • 2
  • 17
  • 27

2 Answers2

1

Svick thanks for the answer, but your approach is binding view model pretty much. I guess this is what the most ASP.NET guys use, because i read about it in few articles. Unfortunately this is not good enough for my site, because I have too many existing code and view models. The better approach is in my opinion, using of Html.RenderAction method, which calls your controller method.

devfreak
  • 1,201
  • 2
  • 17
  • 27
0

If you have the model object, there is an overload of Html.RenderPartial() you can give it to:

Html.RenderPartial("ControlPanel", model);
svick
  • 236,525
  • 50
  • 385
  • 514
  • I know that :), so you are saying the best approach is to use strongly typed master page? – devfreak Nov 13 '10 at 18:18
  • Yeah, possibly using [this method](http://stackoverflow.com/questions/710499/asp-net-mvc-passing-strongly-typed-data-to-master-page/710774#710774). – svick Nov 13 '10 at 18:26
  • I like it, but don't want to bind all my view models to master page model view (inherit from it). Can I use this method without the inherit part? – devfreak Nov 13 '10 at 18:35