Display a view from another controller in ASP.NET MVC

后端 未结 7 1180
孤街浪徒
孤街浪徒 2020-11-29 16:06

Is it possible to display a view from another controller?

Say for example I have a CategoriesController and a Category/NotFound.aspx view.

相关标签:
7条回答
  • 2020-11-29 16:15

    You can also call any controller from JavaScript/jQuery. Say you have a controller returning 404 or some other usercontrol/page. Then, on some action, from your client code, you can call some address that will fire your controller and return the result in HTML format your client code can take this returned result and put it wherever you want in you your page...

    0 讨论(0)
  • 2020-11-29 16:15

    With this code you can obtain any controller:

    var controller = DependencyResolver.Current.GetService<ControllerB>();
    controller.ControllerContext = new ControllerContext(this.Request.RequestContext, 
    controller);
    
    0 讨论(0)
  • 2020-11-29 16:16

    You can use:

    return View("../Category/NotFound", model);
    

    It was tested in ASP.NET MVC 3, but should also work in ASP.NET MVC 2.

    0 讨论(0)
  • 2020-11-29 16:16

    Yes, you can. Return an Action like this :

    return RedirectToAction("View", "Name of Controller");
    

    An example:

    return RedirectToAction("Details/" + id.ToString(), "FullTimeEmployees");
    

    This approach will call the GET method

    Also you could pass values to action like this:

    return RedirectToAction("Details/" + id.ToString(), "FullTimeEmployees", new {id = id.ToString(), viewtype = "extended" });
    
    0 讨论(0)
  • 2020-11-29 16:17

    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.

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

    0 讨论(0)
  • 2020-11-29 16:18

    Have you tried RedirectToAction?

    0 讨论(0)
提交回复
热议问题