开发者

Can you send different view models to a view?

开发者 https://www.devze.com 2023-02-08 05:57 出处:网络
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.

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 FoodViewModel as a base class and have FruitViewModel and VegetableViewModel extend it, you could have ViewPage<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 FruitViewModel and VegetableViewModel is that one is Fruit and one is Vegetable (but all other properties are shared, i.e., Name, Calories, Color, Cost), have a FoodType property on the shared FoodViewModel and use it to conditionally render the appropriate output.


What alternative is the best depends on how much the Fruit and Veg Views differ:

Alternative 1:

You can create two different Views (you can pass the view name to the View method):

public ViewResult Food(int id)
{
    var food = ...
    if (/* determine if food is Veg or Fruit */)
        return View("Fruit", new FruitViewModel { ... });
    else
        return View("Veg", new VegViewModel { ... });
}

By returning a different View the url doesn't change (as it does when using return RedirectToAction("Action", "Controller", ...) which implies a HTTP redirect.

Alternative 2:

You can have a common FoodViewModel extended by both the FruitViewModel and the VegViewModel. Then your FoodView can be of type FoodViewModel and decide what to show in your View code.


If the only thing you need to change is the tabstrip setting. You can provide a property called "ShowTabItem" on your ViewModel, then bind that property to your TabStripItem in your view.

public class FoodViewModel
{
    Food _food;

    public FoodViewModel(Food food)
    {

    }

    public bool ShowTabItem
    {
        get
        {
            return _food.Type == FoodType.Vegetable;
        }
    }
}

Bind your ShowTabItem property to the Visibility or Enabled property of the tabstrip item. (whatever fits)

then your controller will simply be

public ActionResult Food(int id)    
{
    Food food = getFood(id);
    return View(new FoodViewModel(food));
}

Let your ViewModel decide what needs to be displayed.

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号