20

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);
    }
}
Jack Allen
  • 549
  • 2
  • 5
  • 19
user1511069
  • 339
  • 2
  • 5
  • 11
  • This would be so much easier to follow if you displayed the controllers code with both actions that you want to work with. – The Muffin Man Aug 02 '12 at 06:32
  • I'm sorry, I couldn't understand your question. From `ArticlesController` you want to call a method at `CommentsController`. Is that it? – Andre Calil Aug 02 '12 at 06:44

4 Answers4

58

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.

StealthRT
  • 10,108
  • 40
  • 183
  • 342
The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
  • I am Sry for Poorly written question as I am new to .NET . I have updated my ques with code for your ref. – user1511069 Aug 02 '12 at 07:36
  • 4
    `[InvalidOperationException: The view 'Articles/Index' or its master was not found or no view engine supports the searched locations.` I tried this. It looked in /Comments/Articles/Index. Perhaps this is a result of using areas. `return View("../Articles/Index")` worked. – Travis J Sep 04 '12 at 21:07
  • If you're using areas than yes this will not work. Why are you using two different controllers in different areas to access the same view? – The Muffin Man Sep 05 '12 at 07:57
  • 1
    I am not using Area's in my application and still return View("../Articles/Index") worked – Sandesh Daddi Jun 14 '15 at 05:06
  • 2
    You can also start at the root and work the other way if you choose `~/Views/Articles/Index.cshtml` – The Muffin Man Jul 22 '15 at 02:16
2

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

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38
0

I'm not really sure if I got your question right. Maybe something like

public class CommentsController : Controller
{
    [HttpPost]
    public ActionResult WriteComment(CommentModel comment)
    {
        // Do the basic model validation and other stuff
        try
        {
            if (ModelState.IsValid )
            {
                 // Insert the model to database like:
                 db.Comments.Add(comment);
                 db.SaveChanges();

                 // Pass the comment's article id to the read action
                 return RedirectToAction("Read", "Articles", new {id = comment.ArticleID});
            }
        }
        catch ( Exception e )
        {
             throw e;
        }
        // Something went wrong
        return View(comment);

    }
}


public class ArticlesController : Controller
{
    // id is the id of the article
    public ActionResult Read(int id)
    {
        // Get the article from database by id
        var model = db.Articles.Find(id);
        // Return the view
        return View(model);
    }
}
Tomi Lammi
  • 2,096
  • 1
  • 18
  • 12
  • ThankYou...After clicking submit button on write comment form,In debugging my control goes to read.aspx with all valid values but for some reason it is not getting rendered.Where as on writing an article the same Read() function renders read.aspx page properly. – user1511069 Aug 02 '12 at 08:13
  • So Now, In dubugging my control is going till read.aspx but it is not getting displayed..Still write_Comment form is displayed – user1511069 Aug 02 '12 at 08:14
  • The article is found from the database in the read action when you redirect from WriteComment? Does your Read action return the proper model with updated comment? – Tomi Lammi Aug 02 '12 at 08:34
  • public class ArticlesController : AppController { public ActionResult Read(int article) { var output = articleRepository.Find(article); return View(output); } } – user1511069 Aug 02 '12 at 09:02
  • Yes it returns a proper output and return view(output) takes the control to Read.aspx but it is not displayed. – user1511069 Aug 02 '12 at 09:04
  • So are you saying if you put a breakpoint to `return RedirectToAction("Read", "Articles", new {id = comment.ArticleID});` it's not getting hit but returns `return View(comment);` instead? If that's the case then the validation fails or some exception is hit. See the updated answer and check if the method caughts an exception. If everything goes smoothly then there must be something wrong with your view. – Tomi Lammi Aug 02 '12 at 09:19
  • return RedirectToAction("Read", "Articles", new {id = comment.ArticleID}); is getting hit and control moves from CommentController.cs to ArticlesController.cs's Read action just like it happens in normal read My comment form is using Ajax.BeginForm()..I think the prob is something related to it – user1511069 Aug 03 '12 at 10:43
0

It is explained pretty well here: Display a view from another controller in ASP.NET MVC

To quote @Womp:
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.

ASP MVC's idea is "convention over configuration" which means moving the view to the shared folder is the way to go in such cases.

Deitsch
  • 1,610
  • 14
  • 28