asp.net mvc 2多个部分视图

时间:2022-11-30 17:29:15

I have a contoller that renders 3 different views. But I also have a common part (div) for every view. I thought that I can create an UserControl with own controller and include that control on my views (New controller and view as controll).

我有一个渲染三个不同视图的轮廓器。但是我对每个视图都有一个公共部分(div)。我认为我可以用自己的控制器创建一个UserControl,并在我的视图中包含该控件(新控制器和视图作为控件)。

How should I use that UserControl? Should it be a partial view? Or different approach - can I have multiple partial views on one page?

我应该如何使用UserControl?它应该是部分观点吗?或者不同的方法——我可以在一个页面上有多个部分视图吗?

I've been searching the web for the last view days and haven't found working solution that suits me. Also I want to use Strongly Typed views/data.

我一直在网上搜索最后的查看日期,还没有找到适合我的工作方案。我还想使用强类型的视图/数据。

Cheers

干杯

1 个解决方案

#1


2  

You should use a partial view. Then you call <% Html.PartialRender("MyCommonControl", Model); %> in the 3-4 views to render the common section (like a menu or something).

您应该使用部分视图。然后调用<% Html。PartialRender(“MyCommonControl”模型);3-4视图中的%>以呈现公共部分(如菜单或其他)。

This way you can strongly type the partial view and pass the model (like in the above example) or part of the model to it that is relevant.

通过这种方式,您可以强类型局部视图并将模型(如上面的示例)或模型的一部分传递给相关的模型。

UserControls are a ASP.NET Forms paradigm really, you should use partial views because they use the same MVC View Engine.

用户控件是一个ASP。NET Forms范式实际上,您应该使用部分视图,因为它们使用相同的MVC视图引擎。

Update

更新

If you place the PartialView in /Views/Home it'll only be accessible to the HomeController You want to put it in /Views/Common to make it accessible to ALL controllers.

如果您将PartialView放在/Views/Home中,那么它只能被您想要放入/Views/Common中的HomeController访问,以使所有控制器都能访问它。

You should also make a Generic ViewModel for The data that control needs, and make it a sub-component of the models for each Controller:

您还应该为控制需要的数据创建通用视图模型,并将其作为每个控制器模型的子组件:

Eg:

例如:

class CommonSectionViewModel
{
    public string Data { get; set; } // Just Example Data
    public int Count { get; set; }
}

class ProductsModel
{
    public CommonSectionViewModel CommonData { get; set; }
    // Other properties for a products models
}

class CompaniesModel
{
    public CommonSectionViewModel CommonData { get; set; }
    // Other properties for a company model
}

Then in your Views for your controllers you call the partial render like this:

然后在你的视图中对控制器你调用部分渲染如下:

<% Html.PartialView("MyCommonControl", Model.CommonData); %>

Note: You can override the control as well

注意:您还可以重写控件

Having the following files:

有以下文件:

  1. /Views/Common/MyCommonControl.ascx
  2. /视图/普通/ MyCommonControl.ascx
  3. /Views/Products/MyCommonControl.ascx
  4. / /产品/ MyCommonControl.ascx观点

When you call .RenderPartial("MyCommonControl") from ProductsController #2 is used, and from any other controller, #1 is used. So you can override functionality for some controllers if you wish.

当您从ProductsController #2调用. renderpartial(“MyCommonControl”)时,以及从任何其他控制器调用时,都会使用#1。如果你愿意,你可以重写一些控制器的功能。

#1


2  

You should use a partial view. Then you call <% Html.PartialRender("MyCommonControl", Model); %> in the 3-4 views to render the common section (like a menu or something).

您应该使用部分视图。然后调用<% Html。PartialRender(“MyCommonControl”模型);3-4视图中的%>以呈现公共部分(如菜单或其他)。

This way you can strongly type the partial view and pass the model (like in the above example) or part of the model to it that is relevant.

通过这种方式,您可以强类型局部视图并将模型(如上面的示例)或模型的一部分传递给相关的模型。

UserControls are a ASP.NET Forms paradigm really, you should use partial views because they use the same MVC View Engine.

用户控件是一个ASP。NET Forms范式实际上,您应该使用部分视图,因为它们使用相同的MVC视图引擎。

Update

更新

If you place the PartialView in /Views/Home it'll only be accessible to the HomeController You want to put it in /Views/Common to make it accessible to ALL controllers.

如果您将PartialView放在/Views/Home中,那么它只能被您想要放入/Views/Common中的HomeController访问,以使所有控制器都能访问它。

You should also make a Generic ViewModel for The data that control needs, and make it a sub-component of the models for each Controller:

您还应该为控制需要的数据创建通用视图模型,并将其作为每个控制器模型的子组件:

Eg:

例如:

class CommonSectionViewModel
{
    public string Data { get; set; } // Just Example Data
    public int Count { get; set; }
}

class ProductsModel
{
    public CommonSectionViewModel CommonData { get; set; }
    // Other properties for a products models
}

class CompaniesModel
{
    public CommonSectionViewModel CommonData { get; set; }
    // Other properties for a company model
}

Then in your Views for your controllers you call the partial render like this:

然后在你的视图中对控制器你调用部分渲染如下:

<% Html.PartialView("MyCommonControl", Model.CommonData); %>

Note: You can override the control as well

注意:您还可以重写控件

Having the following files:

有以下文件:

  1. /Views/Common/MyCommonControl.ascx
  2. /视图/普通/ MyCommonControl.ascx
  3. /Views/Products/MyCommonControl.ascx
  4. / /产品/ MyCommonControl.ascx观点

When you call .RenderPartial("MyCommonControl") from ProductsController #2 is used, and from any other controller, #1 is used. So you can override functionality for some controllers if you wish.

当您从ProductsController #2调用. renderpartial(“MyCommonControl”)时,以及从任何其他控制器调用时,都会使用#1。如果你愿意,你可以重写一些控制器的功能。