Solved

Loading another controller's view using MVC

Posted on 2011-09-10
5
588 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:TheCommunicator
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 10

Assisted Solution

by:Irzana
Irzana earned 333 total points
ID: 36517894
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
0
 

Author Comment

by:TheCommunicator
ID: 36517932
can you give me an example?
0
 
LVL 10

Accepted Solution

by:
Irzana earned 333 total points
ID: 36517972
0
 
LVL 10

Assisted Solution

by:P1ST0LPETE
P1ST0LPETE earned 167 total points
ID: 36518946
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)".
0
 

Author Comment

by:TheCommunicator
ID: 36525122
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question