简单的形式QUOT;为&QUOT asp.net mvc模式的继承; [英] asp.net mvc model inheritance for "simple forms"
问题描述
你将如何实现这一点?
How would you implement this ?
我有以下型号:
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 \\东西
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应该标签和日期1间去,这种做法是行不通的,当然。这就是为什么它是简单的形式。它是一个很大的节省时间,
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 ?
推荐答案
有一个问题,你会在运行。
There is one problem you will run in to.
在你的行动:公众的ActionResult更新(有什么模型);
如果你调用的UpdateModel(模型)
只在基类字段将被约束。结果
例如,如果模式
是 SomethingStage2
标签
和日期1
将被绑定,但日期2 code>不会。
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.
这是因为尝试/的UpdateModel
运行在编译时的类型,而不是运行时类型。结果
我就遇到了这个问题为好。我在这里张贴的解决方案:结果
<一href=\"http://stackoverflow.com/questions/3257867/mvc-2-updatemodel-on-interface-should-modelbinderattribute-be-ignored\">http://stackoverflow.com/questions/3257867/mvc-2-updatemodel-on-interface-should-modelbinderattribute-be-ignored
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
(我假设在你所有的 Html.EditorFor
语句缺少一个右括号有错别字)
(I'm assuming the missing closing parens in all of your Html.EditorFor
statements are typos)
这篇关于简单的形式QUOT;为&QUOT asp.net mvc模式的继承;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!