在ASP.NET MVC另一个控制器显示视图 [英] Display a view from another controller in ASP.NET MVC

查看:135
本文介绍了在ASP.NET MVC另一个控制器显示视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以显示来自另一控制器的看法?

Is it possible to display a view from another controller?

比方说我有一个 CategoriesController 类别/ NotFound.aspx 视图。而在 CategoriesController ,我可以伊斯利返回视图(NOTFOUND)

Say for example I have a CategoriesController and a Category/NotFound.aspx view. While in the CategoriesController, I can easly return View("NotFound").

现在说我有一个的ProductsController 和行动,以便添加产品。但是,此操作要求我们有一个类别的产品添加到。例如,产品/添加/?CATID = 10

Now say I have a ProductsController and an action and view to add a product. However, this action requires that we have a Category to add the Product to. For example, Products/Add/?catid=10.

如果我无法找到依据 CATID 的范畴,我想说明从 NOTFOUND 视图类别控制器而不是创建产品控制下 CategoryNotFound 视图。

If I am not able to find the Category based on catid, I want to show the NotFound view from the Categories controller instead of creating a CategoryNotFound view under the Products controller.

这是可能的,或者我在错误的方式构建的东西呢?有没有做到这一点的好办法?

Is this possible or am I structuring things in the wrong way? Is there a good way to do this?

推荐答案

是的。默认情况下,ASP.NET MVC检查首先在 \\浏览\\ [Controller_Dir] \\ ,但在那之后,如果它没有找到该视图,它会检查 \\浏览\\共享

Yes. By default, ASP.NET MVC checks first in \Views\[Controller_Dir]\, but after that, if it doesn't find the view, it checks in \Views\Shared.

共享目录有专门为共享多个控制器的意见。只需您查看添加到共享子目录,你是好去。

The shared directory is there specifically to share Views across multiple controllers. Just add your View to the Shared subdirectory and you're good to go.

如果您返回查看(〜/查看/无论/ SomeDir / MyView.aspx)您可以返回任何你想查看。

If you do return View("~/Views/Wherever/SomeDir/MyView.aspx") You can return any View you'd like.

这篇关于在ASP.NET MVC另一个控制器显示视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆