在控制器返回不同的看法 [英] Return different views in a controller
问题描述
如果我有一个控制器和我想要返回基于什么我的条件逻辑去一看,这可能吗?我有不同类型的模型,我要插入到一个视图根据我的条件逻辑(if语句)我能做到这一点?我会怎么做。
If I have a controller and I want to return a view based on what my conditional logic goes to, is that possible? I have different types of models that i want to insert into a view DEPENDING on my conditional logic (if statements) Can i do this? and how would I do this
推荐答案
当然,返回查看()接受一个视图名称作为其第一个参数。只要指定了不同的看法。
Sure, return View() accepts a view name as its first parameter. Just specify a different view.
如果你有不同的模式,进入了同样的观点,无论是尝试将它们合并,创建一个容器模型(每个模型类一个属性然后枚举,这样的看法知道呈现了什么),使用动态作为视图的模型,或创建每个模型一个视图。
If you have different models that go into the same view, either try to merge them, create a container-model (one property per model type and then an enum so that the views know what to render), use dynamic as the model in the view, or create one view per model.
第一个和最后一个会是我的首选,但要看具体情况。
The first and last would be my preferred choice, but it depends on the specifics.
这篇关于在控制器返回不同的看法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!