从不同的控制器调用视图的MVC

20
我的项目结构如下:
  • Controllers/ArticlesController.cs
  • Controllers/CommentsController.cs
  • Views/Articles/Read.aspx

Read.aspx 接收一个参数,“output”,它包含文章 id 和评论,是通过 ArticlesController.cs 传递过来的。

现在我想要在 CommentsController.cs 中编写并读取评论的write()Read() 函数。

为了读取带有其评论的文章,我想从 CommentsController.cs 中调用 Views/Articles/Read.aspx 并传递从 CommentsController.cs 中获取的输出参数。

我该怎么做?

更新

代码如下:

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);
    }
}

如果您能显示控制器代码并包括想要使用的两个操作,那么这将更容易理解。 - The Muffin Man
抱歉,我无法理解你的问题。你想从 ArticlesController 调用 CommentsController 中的一个方法,是这样吗? - Andre Calil
4个回答

58

直接回答您的问题,如果您想返回属于其他控制器的视图,只需指定视图名称和其文件夹名称即可。

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

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

另外,您正在谈论在另一个控制器中使用读写方法。 我认为您应该通过模型直接访问这些方法,而不是调用另一个控制器,因为另一个控制器可能返回HTML。


非常抱歉我的问题写得不好,因为我是.NET的新手。我已经更新了我的问题,并提供了代码供您参考。 - user1511069
4
InvalidOperationException: 找不到视图"Articles/Index"或其母版,或没有视图引擎支持搜索的位置。我尝试了这个。它在/Comments/Articles/Index中查找。也许这是使用区域的结果。使用return View("../Articles/Index")可以解决这个问题。 - Travis J
如果您正在使用区域,则此方法将无法正常工作。为什么要在不同的区域中使用两个不同的控制器来访问相同的视图? - The Muffin Man
1
我在我的应用程序中没有使用区域,但仍然可以使用“return View(“../ Articles / Index”)”返回视图。 - Sandesh Daddi
2
如果你选择~/Views/Articles/Index.cshtml,你也可以从根目录开始并往回工作。 - The Muffin Man

2
您可以将read.aspx视图移动到Shared文件夹中。在这种情况下,这是标准方法。

0

我不太确定我是否正确理解了你的问题。也许类似于这样:

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);
    }
}

谢谢您...在写评论表格上点击提交按钮后,我的控制进入read.aspx页面并传递了所有有效值,但由于某种原因未能被渲染。然而,在撰写文章时,相同的Read()函数可以正常地呈现read.aspx页面。 - user1511069
现在,在调试过程中,我的控件一直进行到read.aspx,但它没有显示出来。仍然显示write_Comment表单。 - user1511069
当您从WriteComment重定向时,文章将在读取操作中从数据库中找到。您的Read操作是否返回带有更新评论的正确模型? - Tomi Lammi
公共类 ArticlesController : AppController { public ActionResult Read(int article) { var output = articleRepository.Find(article); return View(output); } } - user1511069
返回RedirectToAction("Read", "Articles", new {id = comment.ArticleID});被触发,控制权从CommentController.cs移动到ArticlesController.cs的Read操作,就像正常阅读一样。我的评论表单正在使用Ajax.BeginForm()..我认为问题与此有关。 - user1511069
显示剩余2条评论

0

这里解释得很清楚:在ASP.NET MVC中从另一个控制器显示视图

引用@Womp的话:
默认情况下,ASP.NET MVC首先检查\Views\[Controller_Dir]\, 但是如果没有找到视图,则会在\Views\Shared中进行检查。

ASP MVC的理念是“约定优于配置”,这意味着将视图移动到共享文件夹是这种情况下的最佳选择。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接