您可以将不同的视图模型发送到视图吗?
假设我正在实施一个食品视图。 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果将
FoodViewModel
定义为基类,并使用FruitViewModel
和VegetableViewModel
扩展它,则可以使用ViewPage
> 并传入其中之一。然后,您的视图可以检查它获得的特定子类并呈现适当的输出。或者,如果
FruitViewModel
和VegetableViewModel
之间的唯一区别是一个是 Fruit,一个是 Vegetable(但所有其他属性都是共享的,即Name、Calories、 Color、Cost
),在共享的FoodViewModel
上有一个FoodType
属性,并使用它有条件地呈现适当的输出。If you define
FoodViewModel
as a base class and haveFruitViewModel
andVegetableViewModel
extend 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
FruitViewModel
andVegetableViewModel
is that one is Fruit and one is Vegetable (but all other properties are shared, i.e.,Name, Calories, Color, Cost
), have aFoodType
property on the sharedFoodViewModel
and use it to conditionally render the appropriate output.哪种替代方案是最好的取决于水果和蔬菜视图的差异有多大:
替代方案 1:
您可以创建两个不同的视图(您可以将视图名称传递给 View 方法):
通过返回不同的视图,url 不会改变(正如使用
return RedirectToAction("Action", "Controller", ...)
时所做的那样,这意味着 HTTP 重定向。替代方案 2:
您可以使用通用的
FoodViewModel
扩展。通过FruitViewModel
和VegViewModel
然后您的 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):
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 theFruitViewModel
and theVegViewModel
. Then your FoodView can be of typeFoodViewModel
and decide what to show in your View code.如果您唯一需要更改的是选项卡设置。您可以在 ViewModel 上提供一个名为“ShowTabItem”的属性,然后将该属性绑定到视图中的 TabStripItem。
将 ShowTabItem 属性绑定到选项卡条项的 Visibility 或 Enabled 属性。 (无论适合什么)
那么你的控制器将只是
让你的 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.
Bind your ShowTabItem property to the Visibility or Enabled property of the tabstrip item. (whatever fits)
then your controller will simply be
Let your ViewModel decide what needs to be displayed.
Hope this helps!