Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 451 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display a view from another controller in ASP.NET MVC

#1
Is it possible to display a view from another controller?

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

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`.

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?
Reply

#2
You can also call any controller from [JavaScript][1]/[jQuery][2]. 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][3] format
your client code can take this returned result and put it wherever you want in you your page...

[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#3
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.
Reply

#4
Have you tried `RedirectToAction`?
Reply

#5
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.

Reply

#6
Yes its possible.
Return a `RedirectToAction()` method like this:

return RedirectToAction("ActionOrViewName", "ControllerName");

Reply

#7
With this code you can obtain any controller:

var controller = DependencyResolver.Current.GetService<ControllerB>();
controller.ControllerContext = new ControllerContext(this.Request.RequestContext,
controller);
Reply

#8
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" });
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through