我正在使用ASP.NET MVC,并且我有一个模型类,它代表与另一个实体关联但由另一个实体创建的数据。例如,假设下面的ViewModels,无视控制器?
public class User {
public int UserId { get; set; }
public IEnumerable<NewsComment> Comments { get; set; }
}
public class News {
public int NewsId { get; set; }
public string News { get; set; }
public IEnumerable<NewsComment> Comments { get; set; }
}
public class NewsComment {
public int NewsCommentId { get; set; }
public string Comment { get; set; }
public int NewsId { get; set; }
public int UserId { get; set; }
}
public class NewsController : Controller
{
public ActionResult Index()
{
return View(ListAllNews());
}
public ActionResult Detail(int newsId)
{
return View(GetNewsItem(newsId));
}
}
public class NewsCommentController : Controller
{
[AcceptVerbs(HttpVerbs.Post)]
public void Create()
{
int newsId = Request["news_id"];
int userId = Request["user_id"];
string comment = Request["comment"];
SaveNewsComment(newsId, userId, comment);
return RedirectToAction("Detail","News",new { newsId = newsId });
}
}
如果我永远只能显示在新闻/详细信息视图或用户/详细信息视图和评论意见张贴在新闻/详细资料视图
- 是否需要NewsCommentController?
- NewsCommentController应该只有一个Create动作,该动作从该方法调用完成后调用并重定向回NewsController/Detail动作?
- 我应该使用
RenderAction
从NewsCommentController列出我的评论吗? - 或者所有这些都可以在NewsController或UserController中完成,哪一个合适?
什么是“NewsCommentController”?我在你的代码示例中没有看到它。 – 2010-08-04 21:38:11
我正在使用ASP.NET MVC。该模式是一个控制器,负责将数据从模型传递到特定视图,并在模型和视图之间传递变化。 NewsCommentController在这里是一种隐含的类。无论如何,我会扩大。 – 2010-08-04 21:45:00