跨多个部分视图的 ASP.NET MVC 模型

发布于 2024-10-14 19:09:16 字数 1611 浏览 1 评论 0原文

我正在开发一个 MVC 3 Razor Web 应用程序,其中存储了多个类别的对象的详细信息。 (车辆、房屋、仪器等)。所有对象共享一些通用数据(标题、描述等)以及一些特定于其所属类别的详细信息。类别列表预计会增长,并且考虑到可维护性的降低,我们希望重用相同的“添加对象”向导。该向导基于以下实现。

http://afana.me/post/create-wizard-in -aspnet-mvc-3.aspx

在多步骤向导过程中,最后一步允许用户输入类别特定详细信息(车辆的型号、品牌、VIN 等)。目前,我将最后一步设想为使用 AJAX 的部分视图。因此,我们实际上将拥有反映特定类别的多个部分视图,但共享向导代码的其余部分。

我的通用模型对象是这样的

public class AssetView
{
    [Required]      
    public string Title
     {
        get;
        set;
     }

    [Required]
    public string Description
    {
     get;
      set;
     }

    // Few more generic fields here

    public SpecificAsset AssetDetails { get; set; }
}

复杂的属性 AssetDetails 由每种类型的部分视图表示。因此,PartialView“MotorDetails”将包含 MotorAsset 类型的强类型模型,其声明如下。

  public class MotorAsset : SpecificAsset
    {
        [Required]
        public string Transmission
        {
            get;
            set;
        }

        [Required]
        public string Make
        {
            get;
            set;
        }
     }

实际的验证非常复杂,但我省略了这些,以便更容易理解。

主向导页面被声明为

    @model AssetView

  .....

  <div class="wizard-step">
     ....
  </div>
  <div class="wizard-step">
     ....
  </div>
  <div class="wizard-step">
      @{Html.RenderPartial("_MotorCreate", Model.AssetDetails);
  </div>

“电机部分视图”,因为

 @model MotorAsset

我的问题是如何在这种情况下完成模型验证(或者是否可以使用),因为最后一步不在视图页面中,而是在部分视图中。

I'm developing a MVC 3 Razor Web App, in which details of multiple categories of objects are stored. (Vehicles, Houses, Instruments etc) . All the objects share some common data (Title, Description etc) And some details which are specific to the category in which it belongs to. The Category list is expected to grow and in view of reducing maintainability we hope to reuse the same Add Object wizard . The wizard is based on the following implementation.

http://afana.me/post/create-wizard-in-aspnet-mvc-3.aspx

In the Multiple step wizard process , the final step allows the user to enter the category specific details (Model, Make, VIN etc for a Vehicle). Currently i've envisioned this final step as a Partial View with AJAX. So we will actually be having mulltiple partial views that reflect the specific category, but share the rest of the wizard code.

My generic model object is like this

public class AssetView
{
    [Required]      
    public string Title
     {
        get;
        set;
     }

    [Required]
    public string Description
    {
     get;
      set;
     }

    // Few more generic fields here

    public SpecificAsset AssetDetails { get; set; }
}

The complex property AssetDetails is represented by each type of partial view. So the PartialView "MotorDetails" will contain a strongly typed model of type MotorAsset which is declared as below.

  public class MotorAsset : SpecificAsset
    {
        [Required]
        public string Transmission
        {
            get;
            set;
        }

        [Required]
        public string Make
        {
            get;
            set;
        }
     }

The actual validation is far complex , but i've omitted these so that's easier to understand.

The Main Wizard page is declared as

    @model AssetView

  .....

  <div class="wizard-step">
     ....
  </div>
  <div class="wizard-step">
     ....
  </div>
  <div class="wizard-step">
      @{Html.RenderPartial("_MotorCreate", Model.AssetDetails);
  </div>

The Motor Partial View as

 @model MotorAsset

My question is how can i accomplish model validation in this scenario (or is it possible to use), as the final step is not in the view page but on a partial view .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2024-10-21 19:09:16

为什么不采用简单的类继承(OOP 范例)而不是在 AssetView 上拥有属性 AssetDetails?

而不是你现在拥有的

你应该声明AssetView (通用属性),
-> MotorView继承AssetView(特定属性)

像这样:

public class AssetView {     
    [Required]           
    public string Title { get; set; }

    [Required]
    public string Description     { get; set; }      

    // Few more generic fields here
}

public class MotorView : AssetView {
    ...
}

@model MotorView    // you still have access to the AssetView's properties in your view and in your controller actions

如果你想保持现在的样子,那么使用EditorFor()并在共享文件夹中创建一个EditorFor模板并将其强类型为 MotorAsset(或 SpecificAsset,如果您希望它更通用)。

然后像这样渲染它:

@Html.EditorFor(model=>model.AssetDetails)

这将使控制器能够验证它并将其自动绑定到 AssetView 中的属性。
华泰

Why wouldn't you resort to the simple classes inheritance (OOP paradigm) rather than having a property AssetDetails on your AssetView?

Instead of what you now have you should rather declare the

AssetView (generic properties)
-> MotorView inheriting AssetView (specific properties)

Like this:

public class AssetView {     
    [Required]           
    public string Title { get; set; }

    [Required]
    public string Description     { get; set; }      

    // Few more generic fields here
}

public class MotorView : AssetView {
    ...
}

@model MotorView    // you still have access to the AssetView's properties in your view and in your controller actions

If you want to leave it the way you have now, then use EditorFor() and create an EditorFor template in the Shared folder and strong type it to MotorAsset (or SpecificAsset, if you want it even more generic).

Then render it out like this:

@Html.EditorFor(model=>model.AssetDetails)

This will enable the controller to validate it and autobind it to the property in your AssetView.
HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文