Pergunta

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 .

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top