views:

48

answers:

4

Hi,

I have 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

+3  A: 

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
`Model.Reverted` has its value already in controller action. So why in the world complicate the view then? Why can't he just put this `if` into controller action and have two managable views instead of one **bloated**? Why swim against the flow? It's against Asp.net MVC's conceptual model's principle.
Robert Koritnik
+1  A: 

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.

Wyatt Barnett
+4  A: 

Views are supposed to be "stupid"

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.

you can always provide view name when returning views from a controller action:

return View("ViewName", model);

I suggest you analyse and refactor your process.

Robert Koritnik
A: 

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.

eglasius