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 ?