【问题标题】:MVC Calling a view from a different controllerMVC 从不同的控制器调用视图
【发布时间】:2012-08-02 06:22:25
【问题描述】:

我的项目结构是这样的:

  • 控制器/ArticlesController.cs
  • 控制器/CommentsController.cs
  • Views/Articles/Read.aspx

Read.aspx 带一个参数say“output”,就是文章的详细信息,通过id和它的cmets,从ArticlesController.cs传递过来的

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

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

我该怎么做?

更新

代码在这里:

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

【问题讨论】:

  • 如果您在控制器代码中显示您想要使用的两个操作,这将更容易理解。
  • 很抱歉,我无法理解您的问题。您想从ArticlesController 调用CommentsController 的方法。是这样吗?

标签: c# asp.net-mvc


【解决方案1】:

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

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

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

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

【讨论】:

  • 我很抱歉,因为我是 .NET 新手,所以写得不好。我已经用您的参考代码更新了我的问题。
  • [InvalidOperationException: The view 'Articles/Index' or its master was not found or no view engine supports the searched locations. 我试过了。它在 /Comments/Articles/Index 中查找。也许这是使用区域的结果。 return View("../Articles/Index") 工作。
  • 如果您使用的是区域,那么这将不起作用。为什么要在不同区域使用两个不同的控制器来访问同一个视图?
  • 我没有在我的应用程序中使用区域,但仍然返回 View("../Articles/Index") 工作
  • 如果选择~/Views/Articles/Index.cshtml,也可以从root开始,换一种方式工作
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    我不确定你的问题是否正确。也许像

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

    【讨论】:

    • ThankYou...在撰写评论表单上单击提交按钮后,在调试时,我的控件使用所有有效值转到 read.aspx,但由于某种原因它没有被呈现。在写文章时相同的 Read() 函数正确呈现 read.aspx 页面。
    • 所以现在,在调试时,我的控件将持续到 read.aspx 但它没有显示出来..仍然显示 write_Comment 表单
    • 从WriteComment重定向时在read action中从数据库中找到文章?您的读取操作是否返回正确的模型和更新的评论?
    • 公共类 ArticlesController : AppController { public ActionResult Read(int article) { var output = articleRepository.Find(article);返回视图(输出); } }
    • 是的,它返回正确的输出,返回视图(输出)将控制权交给 Read.aspx,但它不显示。
    【解决方案4】:

    这里解释得很好:Display a view from another controller in ASP.NET MVC

    引用 @Womp
    默认情况下,ASP.NET MVC 首先检查\Views\[Controller_Dir]\, 但在那之后,如果它没有找到视图,它会签入\Views\Shared

    ASP MVC 的理念是“约定优于配置”,这意味着在这种情况下,将视图移动到共享文件夹是可行的方法。

    【讨论】:

      猜你喜欢
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多