首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >MVC从不同的控制器调用视图

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

Stack Overflow用户
提问于 2012-08-02 14:22:25
回答 4查看 114.2K关注 0票数 20

我的项目结构如下:

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

Read.aspx接受一个名为"output“的参数,该参数是从ArticlesController.cs传递来的文章的id及其注释的详细信息

现在,我想在CommentsController.cs中编写并阅读comment::write() & Read()函数

为了阅读这篇带有注释的文章,我想通过从CommentsController.cs传递输出参数来从CommentsController.cs调用Views/Articles/Read.aspx

我该怎么做呢?

更新

代码如下:

代码语言:javascript
复制
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);
    }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-08-02 14:46:46

要直接回答您的问题,如果您想返回一个属于另一个控制器的视图,您只需指定该视图的名称及其文件夹名称。

代码语言:javascript
复制
public class CommentsController : Controller
{
    public ActionResult Index()
    { 
        return View("../Articles/Index", model );
    }
}

代码语言:javascript
复制
public class ArticlesController : Controller
{
    public ActionResult Index()
    { 
        return View();
    }
}

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

票数 57
EN

Stack Overflow用户

发布于 2012-08-02 15:22:17

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

票数 2
EN

Stack Overflow用户

发布于 2012-08-02 15:11:01

我不太确定我是否把你的问题弄对了。也许是像这样的

代码语言:javascript
复制
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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11772072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档