我的项目结构如下:
Read.aspx采用一个参数说"输出",这是由id及其注释传递的文章的细节 ArticlesController.cs
现在我想写,然后阅读评论:: write()&Read()funct inCommentsController.cs
对于读其评论文章中,我想打电话Views/Articles/Read.aspx从CommentsController.cs通过从通过输出参数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);
}
}
Run Code Online (Sandbox Code Playgroud)
The*_*Man 54
要直接回答您的问题,如果要返回属于另一个控制器的视图,您只需指定视图的名称及其文件夹名称.
public class CommentsController : Controller
{
public ActionResult Index()
{
return View("../Articles/Index", model );
}
}
Run Code Online (Sandbox Code Playgroud)
和
public class ArticlesController : Controller
{
public ActionResult Index()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
此外,您正在讨论在另一个控制器中使用读写方法.我认为您应该通过模型直接访问这些方法,而不是调用另一个控制器,因为另一个控制器可能返回html.
| 归档时间: |
|
| 查看次数: |
94739 次 |
| 最近记录: |