简体   繁体   English

将强类型控件嵌套到母版页ASP.NET中

[英]Nesting strongly typed control into master page ASP.NET

I have a site using a single master.page. 我有一个使用单个master.page的网站。 In the master.page I have a strongly typed user control (.ascx) called "ControlPanel". 在master.page中,我有一个名为“ ControlPanel”的强类型用户控件(.ascx)。 Its type is ControlPanelViewModel. 它的类型是ControlPanelViewModel。

Here is the mater page - MasterFrame.master: 这是母版页-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: 这是自定义控件页面-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? 现在的问题是如何将正确的ControlPanelViewModel发送到母版页的自定义控件? 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. 我猜这是大多数ASP.NET家伙使用的,因为我在几篇文章中都读过它。 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.RenderAction方法,该方法调用您的控制器方法。

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

Html.RenderPartial("ControlPanel", model);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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