Loading another controller's view using MVC

Hi All,

I want to load another controller's view using MVC. I do it using something ike  return View(~/view/Controller/action);

Now this is a stringly typed view and I am using aspx pages to do that. When it loads, it says that Model is Null. How can I do this?
Who is Participating?
To load another controller's view, you need to call that controller's action. You can use RedirectToAction

and pass all the querystring parameters as route values
TheCommunicatorAuthor Commented:
can you give me an example?
By default when MVC looks for a View, it first looks in \Views\[Controller_Directory]\ .
If MVC can't find the View there, it then looks in \Views\Shared\.

The Views\Shared folder is where you want to put Views that are being used by multiple controllers.

On the other hand, if you simply want to navigate to a different View that is outside the scope of your current controller, you need to do as Irzana posted in his/her first post above; that is, use the RedirectToAction method and pass along all parameters as route values.

To see an example of this - download the example project I put up for you in one of your previous questions:

(i.e. http://davisdevelopment.org/experts-exchange/tasks.zip)

Then go to the HomeController and add this method:

public ActionResult LogOn()
    LogOnModel model = new LogOnModel();
    string url = "www.google.com";

    return RedirectToAction("LogOn", "Account", new { @model = model, @returnUrl = url });

Then run the application and go to the following url /Home/Logon, and you will see that it brings you to Account/Logon.

On a side note, if you are trying to use "return View(view-name)" and you get an error saying Model is null, then the View you are trying to load must have a Model associated with it that it needs to fully populate it's content.  So you need to find out what that model is and pass it to the view using "return View(view-name, model)".
TheCommunicatorAuthor Commented:
Hi Guys,

Instead of doing that you have suggeste, i ended up doing something like below:

return View("~/,,,", Model)

Open in new window

That actually seemed to me like a simple solution.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.