My project structure is like:
我的项目结构是:
- Controllers/ArticlesController.cs
- 控制器/ ArticlesController.cs
- Controllers/CommentsController.cs
- 控制器/ CommentsController.cs
- Views/Articles/Read.aspx
- 视图/文章/ 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
阅读。aspx接受一个名为“output”的参数,该参数是通过id及其注释传递的文章的详细信息,该参数来自ArticlesController.cs
Now I want to write then read the comment:: write()
& Read()
funct in CommentsController.cs
现在我想写,然后读comments .cs中的write()和read()函数
For reading the article with its comments, I want to call Views/Articles/Read.aspx
from CommentsController.cs
by passing output parameter from CommentsController.cs
要阅读带有评论的文章,我想调用视图/文章/阅读。aspx CommentsController。通过从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);
}
}
3 个解决方案
#1
45
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时调用另一个控制器。
#2
1
You can move you read.aspx view to Shared folder. It is standard way in such circumstances
你可以移动你的阅读。aspx视图到共享文件夹。这是在这种情况下的标准做法
#3
0
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);
}
}
#1
45
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时调用另一个控制器。
#2
1
You can move you read.aspx view to Shared folder. It is standard way in such circumstances
你可以移动你的阅读。aspx视图到共享文件夹。这是在这种情况下的标准做法
#3
0
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);
}
}