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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11772072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:10:59  来源:igfitidea点击:

MVC Calling a view from a different controller

c#asp.net-mvc

提问by user1511069

My project structure is like:

我的项目结构是这样的:

  • Controllers/ArticlesController.cs
  • Controllers/CommentsController.cs
  • Views/Articles/Read.aspx
  • 控制器/文章Controller.cs
  • 控制器/评论Controller.cs
  • 视图/文章/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

Read.aspx 有一个参数说“输出”,它是通过 id 和它的评论的文章的详细信息,从 ArticlesController.cs

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

现在我想写然后读评论:: write()&Read()函数CommentsController.cs

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

对于读其评论文章中,我想打电话Views/Articles/Read.aspxCommentsController.cs通过从通过输出参数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);
    }
}

采纳答案by The Muffin Man

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.

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

回答by Tomi Lammi

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

回答by Kirill Bestemyanov

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

您可以将 read.aspx 视图移动到共享文件夹。在这种情况下这是标准方式