I have a MVC 3 Razor web a开发者_如何转开发pplication that needs to allow creating and editing of lots of models that are subclasses.
For example say I have the following object model:
Vehicle
- Car
- Truck
- Van
Vehicle provides many common attributes, then the subclasses add their own specific ones. I would like to create a tabbed interface for creating and editing these objects. I don't want to create a different page for each object type though.
What I have done in the past in Windows Forms is create a base form that has controls for the base object, then an empty tab that I dynamically load a control onto depending on the object type. All of the controls implement an interface with methods that take the base class as an argument, then they cast it to their specific type and display/update the object properties.
What is the best way to implement something like this in Razor?
Thanks!
You could do worse than have a look at Partial Views. Here's some links:
http://jeffreypalermo.com/blog/asp-net-mvc-and-the-templated-partial-view-death-to-ascx/
http://www.mikesdotnetting.com/Article/105/ASP.NET-MVC-Partial-Views-and-Strongly-Typed-Custom-ViewModels
Effectively, you would define your "common" layout separately (the fields that pertain to all vehicles). And then use a strongly-typed RenderPartial
to render it in each of the vehicle views. These vehicle views would therefore only contain those things that are relevant to the specific vehicle sub-class that they represent.
精彩评论