views:

61

answers:

1

How would you implement this ?

I have the following model :

class Something
{
     public string Label { get; set; }
     public DateTime Date1 { get; set; }
}

class SomethingStage2 : Something
{
     public DateTime Date2 { get; set; }
}

class SomethingStage3 : SomethingStage2
{
     public DateTime Date3 { get; set; }
}

And the following editor templates :

EditorTemplates\Something

<%@ Control Language="C#" Inherits="ViewUserControl<Something>" %>
<%= Html.Hidden( "TypeName", Model.GetType() ) %>
<%= Html.EditorFor( x => x.Label ) %>
<%= Html.EditorFor( x => x.Date1 ) %>

EditorTemplates\SomethingStage2

<%@ Control Language="C#" Inherits="ViewUserControl<SomethingStage2>" %>
<% Html.RenderPartial("EditorTemplates/Something.ascx" %>
<%= Html.EditorFor( x => x.Date2 ) %>

EditorTemplates\SomethingStage3

<%@ Control Language="C#" Inherits="ViewUserControl<SomethingStage3>" %>
<% Html.RenderPartial("EditorTemplates/SomethingStage2.ascx" %>
<%= Html.EditorFor( x => x.Date3 ) %>

For updating, I have the following controller method :

public ActionResult Update( Something model );

Technically, it works very well.

Update : for handling the different subclasses of the model, I've borrowed this idea : http://www.codinginstinct.com/2010/03/aspnet-mvc-and-convention-based-forms.html

public class CustomModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var typeName = bindingContext.ValueProvider.GetValue("TypeName");
        var type = Type.GetType(reportFormTypeName.AttemptedValue);
        var model = bindingContext.ModelMetadata.Model;
        bindingContext.ModelMetadata = new ModelMetadata(ModelMetadataProviders.Current, 
            bindingContext.ModelMetadata.ContainerType,
            () => model, type , bindingContext.ModelMetadata.PropertyName);

        return base.BindModel(controllerContext, bindingContext);
    }
}

Update : If Date3 should go between Label and Date1, this approach won't work, of course. That's why it is for simple forms. And it is a big time saver,

Is this approach correct, for simple cases, where edit forms are straightforward ? I'm just wondering if it is "right" to do it this way. If not, how would you implement this ?

A: 

There is one problem you will run in to.

In your action: public ActionResult Update( Something model ); if you call UpdateModel(model) only the fields in the base class will be bound.
For example, if model is a SomethingStage2 Label and Date1 will be bound but Date2 will not.

This is because Try/UpdateModel operates on the compile time type and not the runtime type.
I ran into this issue as well. I posted the solution here:
http://stackoverflow.com/questions/3257867/mvc-2-updatemodel-on-interface-should-modelbinderattribute-be-ignored

(I'm assuming the missing closing parens in all of your Html.EditorFor statements are typos)

jwsample
you're right. I've added the code. Got it in my application tough :)
mathieu