我正在使用ASP.NET MVC,我有一个模型类,它代表一个与实体关联但由另一个实体创建的数据。例如,给定以下ViewModel,
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 });
}
}
如果我只在新闻/详细信息视图或用户/详细信息视图上显示评论,评论会在新闻/详细信息视图中发布,那么
RenderAction
从NewsCommentController列出我的评论吗?答案 0 :(得分:2)
如果您的新闻/详情视图发布了新评论,那么您无需拥有NewsCommentController。您的NewsComment
类本身不是ViewModel。