简体   繁体   中英

Nesting strongly typed control into master page ASP.NET

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?

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.

如果有模型对象,则可以Html.RenderPartial()重载为:

Html.RenderPartial("ControlPanel", model);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM