我如何在我的 asp.net mvc web 应用程序中创建一个可以接受不同类型对象的共享局部视图

how i can create a shared partial view inside my asp.net mvc web application that can accept different types of objects

我正在开发 asp.net MVC 网络 application.and 我有很多模型 classes 代表服务器、vm、PC、监视器等。对于这些模型中的每一个classes 有一个共享 class 用于填充第三方 API。所以我使用共享 class 扩展了我所有的模型 classes,如下所示:-

 public class Server : CreateResource,  IValidatableObject
    {//code goes here}

 public class VM : CreateResource,  IValidatableObject
    {//code goes here}
 public class PC : CreateResource,  IValidatableObject
    {//code goes here}

这里是 CreateResource class:-

public class CreateResource
    {
        public CreateResource()
        {
            this.operation = new Operation5();
            this.createAccount = new CreateAccount();
        }
        public Operation5 operation { get; set; }
        public CreateAccount createAccount { get; set; }

    }

现在我面临的问题是,对于所有模型 classes ,我将在创建服务器、虚拟机、PC 对象时使用准确的视图来输入 CreateResource 数据。所以在服务器、vm、pc 等主 create/edit 视图中,我添加了对部分视图的引用,如下所示(这是服务器对象的示例):-

@model S.Models.Server

@Html.Partial("_PMCreateResource",Model.operation.Details)
    @Html.Partial("_PMCreateAccount",Model.createAccount.operation.Details.ACCOUNTLIST.ToList())

但我面临的问题是,当视图被 posted 返回到 Create/Edit 操作方法时,我必须定义单独的参数来访问 posted 返回的模型(一个主模型和2个代表部分视图的模型)如下(这是服务器操作方法的示例):-

[HttpPost]       
[ValidateAntiForgeryToken]
public ActionResult Create(Server sj,Details4 d4,List<ACCOUNTLIST> al)
{

并且只能按如下方式定义服务器对象:-

[HttpPost]       
[ValidateAntiForgeryToken]
public ActionResult Create(Server sj)
{

然后我需要将整个模型传递给局部视图,如下所示(这是服务器主视图的示例):-

@model S.Models.Server

@Html.Partial("_PMCreateResource",Model)
@Html.Partial("_PMCreateAccount",Model)

但是将整个服务器、vm、pc、监视器模型传递给相同的局部视图意味着我必须为每个模型创建单独的局部视图 class,因为每个局部视图将接受不同的模型对象.所以不确定我是否可以修改我的代码来实现这两件事:-

  1. 将整个模型对象传递给部分 view.so 当 post 返回视图时 post 操作方法将只接受一个模型对象作为参数,而不是 3 个参数.
  2. 使用单个共享视图,它将接受不同类型的模型对象?

不确定我该如何实现?

不要使用 @HtmlPartial(),请使用 EditorTemplate 以便正确地为控件的 name 属性添加前缀。

/Views/Shared/EditorTemplates文件夹中创建一个名为CreateResource.cshtml的局部视图(注意文件名必须与class的名称匹配)

@model CreateResource
@Html.TextboxFor(m => m.operation.Details.SomeProperty)
....
for(int i = 0; i < Model.createAccount.operation.Details.ACCOUNTLIST.Count; i++)
{
    @Html.TextBoxFor(m => m.createAccount.operation.Details.ACCOUNTLIST[i].SomeProperty)
    ....
}

然后在主视图

@model S.Models.Server
Html.EditorFor(m => m) // generated the base controls
.... // controls specific to Server

然后将 EditorTemplate 分解为更易于管理的部分并允许您重复使用它们,为 Operation5CreateAccount

创建额外的模板

/Views/Shared/EditorTemplates/Operation5.cshtml

@model Operation5
@Html.TextBoxFor(m => m.Details.SomeProperty)
....

并将 CreateResource.cshtml 模板更改为

@model CreateResource
@Html.EditorFor(m => m.operation)
@Html.EditorFor(m => m.createAccount)

你可以继续分解它为每个嵌套模型创建一个 EditorTemplate,包括集合项,所以假设 属性 Details 是 typeof Detail并且 属性 ACCOUNTLIST 是 typeof List<AccountItem>,那么你会得到

/Views/Shared/EditorTemplates/AccountItem.cshtml

@model AccountItem
@Html.TextBoxFor(m => m.SomeProperty)

/Views/Shared/EditorTemplates/Detail.cshtml

@model Detail
@Html.TextBoxFor(m => m.SomeProperty)
@Html.EditorFor(m => m.ACCOUNTLIST) // generates the html for each item in the collection