“简单形式"的asp.net mvc模型继承 [英] asp.net mvc model inheritance for "simple forms"
问题描述
您将如何实现这一点?
我有以下模型:
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; }
}
以及以下编辑器模板:
EditorTemplates\Something
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
EditorTemplates\SomethingStage2
<%@ Control Language="C#" Inherits="ViewUserControl<SomethingStage2>" %>
<% Html.RenderPartial("EditorTemplates/Something.ascx" %>
<%= Html.EditorFor( x => x.Date2 ) %>
EditorTemplates\SomethingStage3
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.
更新:为了处理模型的不同子类,我借用了这个想法:http://www.codinginstinct.com/2010/03/aspnet-mvc-and-convention-based-forms.html
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);
}
}
更新:如果 Date3 应该在 Label 和 Date1 之间,这种方法当然行不通.这就是为什么它适用于简单的表格.这是一个很大的节省时间,
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 ?
推荐答案
您会遇到一个问题.
在您的操作中:public ActionResult Update(Something model);
如果您调用 UpdateModel(model)
,则只会绑定基类中的字段.
例如,如果 model
是 SomethingStage2
Label
并且 Date1
将被绑定但 Date2
> 不会.
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.
这是因为 Try/UpdateModel
对编译时类型而不是运行时类型进行操作.
我也遇到了这个问题.我在这里发布了解决方案:
接口上的MVC 2 UpdateModel,是否应该忽略ModelBinderAttribute?
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:
MVC 2 UpdateModel on Interface, Should ModelBinderAttribute be ignored?
(我假设您所有 Html.EditorFor
语句中缺少的右括号都是拼写错误)
(I'm assuming the missing closing parens in all of your Html.EditorFor
statements are typos)
这篇关于“简单形式"的asp.net mvc模型继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!