views:

58

answers:

3

For instance I have a model X with properties Title(string) and Valid(bool). I need to show same model on two separate pages with different field labels and input controls. E.g. "Title" for title and "Valid" for valid on one form while "Destination" for title and "Returning" for valid on the other.

I guess the easiest way would be to have two different views for the same model. But is it really a MVC way to go?

Thanks

+1  A: 

Is it really the same model?

If they're two different entities with similar properties then I would create two separate view models. Any commonality could be put in an abstract base class or interface.

If it's the same model but just a different input screen then sure, reuse the model.

I would imagine the first case is probably the one that is relevant here.

nukefusion
Yeah, it IS the same model from technical view. But at the same time there are two models from business point of view. And yes I know it sounds like crap but that's the way it is.
Ramunas
+3  A: 

Well, let's say you have some View-folder called List, and one called Details - and displaying the Model in the two should be different.

You can create a DisplayTemplates folder within each of the two folders, and create a PartialControl with the same name as your Model, and also strongly type it to your Model.

In your different views you can then do <%= Html.DisplayFor( your model) %> or you can also use the regular <% Html.RenderParital("NameOfPartial", ModelX); %>

Edit To try and approach the original question, maybe this could help you in some way (I posted this as an answer to a different question How to change [DisplayName“xxx”] in Controller?)

public class MyDisplayName : DisplayNameAttribute
{
    public int DbId { get; set; }

    public MyDisplayName(int DbId)
    {
        this.DbId = DbId;
    }


    public override string DisplayName
    {
        get
        {
            // Do some db-lookup to retrieve the name
            return "Some string from DBLookup";
        }
    }
}

    public class TestModel
    {
        [MyDisplayName(2)]
        public string MyTextField { get; set; }
    }

Maybe you could rewrite the custom-attribute to do some sort of logic-based Name-selection, and that way use the same PartialView for both model-variations?

Yngve B. Nilsen
Yeah, that's the way I've had implemented this. But what made me to ask this questions is that I loose possibility to use data annotations and the charm it gives me. I was hoping that someone will elaborate on using some kind of intermediate class for one of my views. Maybe it is more complicated but a MVC way to do stuff like this?
Ramunas
And one more thing about folders. It's not like representing same model in List and Details views, it's more like representing same model in Controller1/Edit/1 and Controller2/Edit/2 views. Technically it is the same model representing two different business entities (like Product and Route in my trivial example)
Ramunas
To be perfectly honest, I'd drop using the same model for two totally different entity objects. What happens if you decide to make the Product slightly different than the Route later on? You'd have to either 1. Make the changes the same for both - or - 2. Create a new model and go through your code changing everywhere you use Route for Product or vice versa. As you say, you don't want to lose the charm of annotations, and I totally agree - so I don't really see any advantages of using the same model for two different entities.
Yngve B. Nilsen
+2  A: 

Yes, two different Views is appropriate, as you are providing two different VIEWS of your MODEL.

However, are you sure you aren't shoehorning your data into a single model, when in fact it represents a different entity in each case?

Jamie
See my comments to Yngve B. Nilsen's answer, please. Yes, you're right, I am using one model for different entities, but it's the only way out of getting involved in refactoring an enterprise solution for a year or so.
Ramunas