我可以使用从其他控制器为主控制器定义的视图吗? [英] Can I use the view which has been defined for main Controller from other controller?

查看:79
本文介绍了我可以使用从其他控制器为主控制器定义的视图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在控制器中实例化了一个控制器。我在主控制器下定义了一个视图。现在,当我尝试使用其他控制器的视图时,它给出了一个错误,说明找不到。





示例:

public HomeController:Controller

{

}

public OtherController:Controller

{

}



查看已创建:用户



现在当我尝试通过OtherController使用User,路径原来是OtherController / User,它给出了一个错误,说找不到View,因为View已经为HomeController定义了。



那么,有什么方法可以从其他控制器访问用户视图。



任何帮助或想法都表示赞赏。



如果有什么不清楚,请告诉我。

谢谢。

Hi All,

I have instantiated a Controller within a Controller. And I have a view defined under the main Controller. Now when I try to use the view from the other controller, it gives me an error saying View not found.


Example:
public HomeController: Controller
{
}
public OtherController: Controller
{
}

View Created: User

Now when I try to use "User" via OtherController, the path turns out to be OtherController/User and it gives me an error saying View not found which is right because that View has been defined for HomeController.

So, is there any way I can access User view from Other Controller.

Any help or idea is appreciated.

Do let me know if anything is unclear.
Thanks.

推荐答案

你只需说明一下在return语句中查看名称:



You just have to state that view name in your return statement:

public ActionResult SomeAction()
{
    // do something
    return View("SharedView", viewModel);
}



如果您有一个名为SharedView的视图,则该控制器中的所有操作都可以访问它。如果要在其他控制器上的操作之间共享它,可以使用视图中的共享文件夹或使用视图名称声明完整路径。

希望这有帮助


If you have a view named SharedView, all actions in that controller can access it. If you want to share it among actions that are on other controllers, you can use the Shared folder in Views or state full path with the view name.
Hope this helps


这篇关于我可以使用从其他控制器为主控制器定义的视图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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