Page needs to inherit 2 view models and show whichever model thats required
I hav开发者_运维知识库e a content page which is quite happy to show inherit a contentModel. I need to change the page so it can also show another viewModel (e.g. revertedContent), but only show it if it needs to other show the normal contentModel.
I'm sure there is a simple way to do this it's just a pain as I need to ensure the page doesn't get changed in the url e.g. details.aspx stays as is but can show current content or previous versioned content.
cheers in advance
Views are supposed to be "stupid" (and have single responsibility)
I don't think you fully understand the Asp.net MVC conceptual model. Views are supposed to be stupid and have just as much logic in them as needed. Divide and conquer is the rule here. So if you have two different views of a particular data, you'd build two customised views as well.
Controller's supposed to be the smart guy here. So giving views the possibility of decisions isn't the right way of doing it.
Decision is almost certainly based on application model state anyway, so it's up to controller to decide which view to display and provide the correct model for that particular view.
It's nothing uncommon to return various views from the same controller action. It's not written in stone that each controller action should have one view. This way we'd get bloated views with too much code hence making them unmaintainable. Basically we'd blow the whole separation of concerns of the MVC model.
So when you'd like to return particular view from your controller you can always provide its name when returning from the controller action:
return View("ViewName", model);
I suggest you analyse and refactor your process.
Can you create a "parent" View Model instead which contains both contentModel
and revertedContent
objects? Send this new View Model to the view, and check whether the revertedContent member is null.
public class ParentViewModel
{
public contentModel content { get; set; }
public revertedContent reverted { get; set; }
}
Then the view
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<YourNamespace.ParentViewModel>" %>
...
<% if(!Model.reverted) { %>
//do regular content here
<% } else { %>
//do reverted content here
<% } %>
David's answer is one direction. I'd look at doing something a little higher up the food chain--like make the controller pick the view as well as supplying a viewmodel.
I suggest to change your view models design to allow for the scenario, instead of having 2 unrelated view models, make sure both fit under the same type.
Only you will know which design will make sense for your application.
I'll take a blind guess, and suggest you can always use ContentViewModel in your view. Have a IsRevertedInfo property in it, that you can check in the view to display any extra information.
精彩评论