您可以将不同的视图模型发送到视图吗?

发布于 2024-10-15 23:50:40 字数 765 浏览 3 评论 0原文

假设我正在实施一个食品视图。 (ASP.NET MVC2)

然后根据类型(例如水果或蔬菜)我将更改视图。

我可以在不为水果和蔬菜创建单独的视图的情况下执行此操作吗?

也就是说,我们有像 http://localhost:xxxx/Food/{foodID} 这样的 url 结构,

但没有想要

http://localhost:xxxx/Veg/{foodID}

http://localhost:xxxx/Fruit/{foodID}

所以我希望能够根据类型更改视图。我正在使用 Telerik 的 tabstrip 控件,让您了解视图中的差异 - 只是说 - 不显示蔬菜的特定选项卡,而显示水果等。

一个视图可以接受两个不同的视图模型吗?因此,当我们点击 http://localhost:xxxx/Food/{foodID} 时,代码会确定对象的类型是(水果或蔬菜)然后发送 FruitViewModel 或 VegetableViewModel 吗?如果我只发送一个视图模型,如何控制在视图中显示或不显示某些内容的逻辑?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蒗幽 2024-10-22 23:50:40

如果将 FoodViewModel 定义为基类,并使用 FruitViewModelVegetableViewModel 扩展它,则可以使用 ViewPage > 并传入其中之一。然后,您的视图可以检查它获得的特定子类并呈现适当的输出。

或者,如果 FruitViewModelVegetableViewModel 之间的唯一区别是一个是 Fruit,一个是 Vegetable(但所有其他属性都是共享的,即 Name、Calories、 Color、Cost),在共享的 FoodViewModel 上有一个 FoodType 属性,并使用它有条件地呈现适当的输出。

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.

﹎☆浅夏丿初晴 2024-10-22 23:50:40

哪种替代方案是最好的取决于水果和蔬菜视图的差异有多大:

替代方案 1:

您可以创建两个不同的视图(您可以将视图名称传递给 View 方法):

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 { ... });
}

通过返回不同的视图,url 不会改变(正如使用 return RedirectToAction("Action", "Controller", ...) 时所做的那样,这意味着 HTTP 重定向。

替代方案 2:

您可以使用通用的 FoodViewModel 扩展。通过 FruitViewModelVegViewModel 然后您的 FoodView 可以是 FoodViewModel 类型并决定在您的视图代码中显示什么。

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.

桃扇骨 2024-10-22 23:50:40

如果您唯一需要更改的是选项卡设置。您可以在 ViewModel 上提供一个名为“ShowTabItem”的属性,然后将该属性绑定到视图中的 TabStripItem。

public class FoodViewModel
{
    Food _food;

    public FoodViewModel(Food food)
    {

    }

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

将 ShowTabItem 属性绑定到选项卡条项的 Visibility 或 Enabled 属性。 (无论适合什么)

那么你的控制器将只是

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

让你的 ViewModel 决定需要显示什么。

希望这有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文