Say I am implementing a view for Food. (ASP.NET MVC2)
Then depending on the type (say fruit or vegetable for example) I will change the view.
Can I do this without creating a seperate view for fruit and vegetable?
I.e. Say we have url structure like http://localhost:xxxx/Food/{foodID}
and don’t want
http://localhost:xxxx/Veg/{foodID}
http://localhost:xxxx/Fruit/{foodID}
So I want to be able to change the view depending on the type. I’m using a tabstrip control from telerik, to give you an idea of the difference in the views – it’d just be say – not displaying one particular tab for Veg, and displaying it if fruit, for example.
Can a view accept two different view models ? so when we hit http://localhost:xxxx/Food/{foodID} the code determines what type the object is (Fruit or Vegetable) then sends either FruitViewModel or VegetableViewModel ? If I just send one viewmodel how can I control the logic to display or not display certain things in the view?
If you define
FoodViewModelas a base class and haveFruitViewModelandVegetableViewModelextend it, you could haveViewPage<FoodViewModel>and pass in either. Then, your view can check for which specific subclass it got and render the appropriate output.Alternatively, if the only difference between
FruitViewModelandVegetableViewModelis that one is Fruit and one is Vegetable (but all other properties are shared, i.e.,Name, Calories, Color, Cost), have aFoodTypeproperty on the sharedFoodViewModeland use it to conditionally render the appropriate output.