Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Calling a view from a different controller

Tags:

My project structure is like:

  • Controllers/ArticlesController.cs
  • Controllers/CommentsController.cs
  • Views/Articles/Read.aspx

Read.aspx takes a parameter say "output", which is the details of the article by id and its comments, passed from ArticlesController.cs

Now I want to write then read the comment:: write() & Read() funct in CommentsController.cs

For reading the article with its comments, I want to call Views/Articles/Read.aspx from CommentsController.cs by passing output parameter from CommentsController.cs

How can I do this?

UPDATE

Code Here:

public class CommentsController : AppController
{
    public ActionResult write()
    {
        //some code
        commentRepository.Add(comment);
        commentRepository.Save();

        //works fine till here, Data saved in db
        return RedirectToAction("Read", new { article = comment.article_id });
    }

    public ActionResult Read(int article)
    {   
        ArticleRepository ar = new ArticleRepository();
        var output = ar.Find(article);

        //Now I want to redirect to Articles/Read.aspx with output parameter.
        return View("Articles/Read", new { article = comment.article_id });
    }
}

public class ArticlesController : AppController
{   
    public ActionResult Read(int article)
    {
        var output = articleRepository.Find(article);

        //This Displays article data in Articles/Read.aspx
        return View(output);
    }
}
like image 877
user1511069 Avatar asked Aug 02 '12 06:08

user1511069


People also ask

How can we call a view from another controller in MVC?

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.

Can two different controllers access a single view in MVC?

Yes. Mention the view full path in the View method. If the name of your Views are same in both the controllers, You can keep the Common view under the Views/Shared directory and simply call the View method without any parameter.

How do I redirect a view from a controller?

You can use the RedirectToAction() method, then the action you redirect to can return a View. The easiest way to do this is: return RedirectToAction("Index", model); Then in your Index method, return the view you want.


2 Answers

To directly answer your question if you want to return a view that belongs to another controller you simply have to specify the name of the view and its folder name.

public class CommentsController : Controller
{
    public ActionResult Index()
    { 
        return View("../Articles/Index", model );
    }
}

and

public class ArticlesController : Controller
{
    public ActionResult Index()
    { 
        return View();
    }
}

Also, you're talking about using a read and write method from one controller in another. I think you should directly access those methods through a model rather than calling into another controller as the other controller probably returns html.

like image 151
The Muffin Man Avatar answered Sep 26 '22 02:09

The Muffin Man


You can move you read.aspx view to Shared folder. It is standard way in such circumstances

like image 42
Kirill Bestemyanov Avatar answered Sep 23 '22 02:09

Kirill Bestemyanov