开发者

Dynamic in MVC Views with similar but not the same classes as Model

Let's say we have an ASP.NET MVC View like this:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%: Html.EditorFor(model => model.ServiceDate) %>
<%: Html.ValidationMessageFor(model => model.ServiceDate, "*")%>

and we have Model classes that are similar but not exactly the same, for instance Invoice and Quote, which both contain the ServiceDate property. Those two classes do not inherit from the same base class, actually they have no base class currently.

  1. How can I get them to inherit from the same base class when those two are generated by EF? If I would be able to do that, I could replace the dynamic and specify the base class as the View's strong type.

  2. I was under impression that this wa开发者_运维问答s the case that dynamic was built for. But obviously it does not work because there's an exception that's apparently coming from LINQ to EF:

    CS1963: An expression tree may not contain a dynamic operation

Although it does say in the VS 2010 editor that expression will be resolved at runtime, it is not, but fails in error.


1) Classes generated by Entity Framework use the partial keyword. This means you can easily extend them and not worry about code generation wiping out your changes.

So lets say EF generates: public partial class Customer

What you can do is define another .cs file called Customer_Partial.cs ( just my personal naming preference ) and then do something like:

 public partial class Customer : IMySharedInterface
 {  
 }

Then your view pages use:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IMySharedInterface>" %>

and then access your model properties in a strongly typed way.

2) I think this is caused by you passing an IQueryable into your view. Not sure though. With the above solution you won't have to worry about it.


First of all, which EF version are you using? I suppose your using EFv1, right? You can add inheritance to your models that come from EF. They are all defined as parcial classes by the framework. That means you can create another class with the same name (also parcial) that inherits from the base class.

By the way, I recommend you to use ViewModels in that case. Avoid using inheritance just to reuse some properties, inheritance should be used to add polymorphic behaviour to your classes.


It seems that you are trying to use classes generated by EF in your views. I would recommend you not doing this. That's what ViewModels are supposed to do. Don't be afraid to create a view model for each view (even if you had to repeat some properties) and avoid passing the Model. Also having views that are strongly typed to <dynamic> is as if you had weakly typed views. Once again: avoid them:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SomeAdaptedViewModel>" %>
<%: Html.EditorFor(model => model.ServiceDate) %>
<%: Html.ValidationMessageFor(model => model.ServiceDate, "*")%>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜