Asp.net mvc 如何使用MVC视图和PartialView显示嵌套数据

Asp.net mvc 如何使用MVC视图和PartialView显示嵌套数据,asp.net-mvc,Asp.net Mvc,我正在处理的问题与StackOverflow显示问题、评论、帖子以及与帖子相关的评论的方式非常相似。层次结构是相同的 这是如何在ASP.Net MVC中实现的? 到目前为止,我有以下内容:(我已将文件命名为类似于So站点,以使我的问题更具可读性) 视图/问题/详细信息.aspx public class QuestionsController : Controller { public ActionResult Details(int? questionId) {

我正在处理的问题与StackOverflow显示问题、评论、帖子以及与帖子相关的评论的方式非常相似。层次结构是相同的

这是如何在ASP.Net MVC中实现的?

到目前为止,我有以下内容:(我已将文件命名为类似于So站点,以使我的问题更具可读性)

视图/问题/详细信息.aspx

public class QuestionsController : Controller
{
    public ActionResult Details(int? questionId)
    {
        Question question= _db.Question .First(i => i.QuestionId== questionId);
        return View(question);
    }
}
这将加载详细信息并显示问题


我有一个名为QuestionComment的用户控件,它应该显示问题的注释,但我不知道如何连接它。我一直在使用晚餐解决方案作为指导。

在你看来,写这样的东西

<% foreach (var question in Model.Questions) { %>

<%=question.bodyText%>

<%}%>


希望这有帮助,如果不发表评论的话,我就不会那么神秘了。

在你看来,写一些这样的东西

<% foreach (var question in Model.Questions) { %>

<%=question.bodyText%>

<%}%>


希望这有帮助,如果不发表评论的话,我就不会那么神秘了。

创建ViewModel以显示带有评论的问题。大概是这样的:

public class QuestionViewModel
{
    public Question Question { get; set; }
    public IEnumerable<Comment> Comments { get; set; }
}
您的“详细信息”视图:


“问题控制”部分视图:

<%@ Control Inherits="System.Web.Mvc.ViewUserControl<Question>" %>

<h3><%= Model.Title %></h3>

...
<%@ Control Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Comment>>" %>

<ul>
<% foreach (var comment in Model) { %>
    <li>
        <%= comment.Content %>
    </li>
<% } %>
</ul>

...

...
“CommentsControl”部分视图:

<%@ Control Inherits="System.Web.Mvc.ViewUserControl<Question>" %>

<h3><%= Model.Title %></h3>

...
<%@ Control Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Comment>>" %>

<ul>
<% foreach (var comment in Model) { %>
    <li>
        <%= comment.Content %>
    </li>
<% } %>
</ul>

...

...
创建视图模型以显示带注释的问题。大概是这样的:

public class QuestionViewModel
{
    public Question Question { get; set; }
    public IEnumerable<Comment> Comments { get; set; }
}
您的“详细信息”视图:


“问题控制”部分视图:

<%@ Control Inherits="System.Web.Mvc.ViewUserControl<Question>" %>

<h3><%= Model.Title %></h3>

...
<%@ Control Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Comment>>" %>

<ul>
<% foreach (var comment in Model) { %>
    <li>
        <%= comment.Content %>
    </li>
<% } %>
</ul>

...

...
“CommentsControl”部分视图:

<%@ Control Inherits="System.Web.Mvc.ViewUserControl<Question>" %>

<h3><%= Model.Title %></h3>

...
<%@ Control Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Comment>>" %>

<ul>
<% foreach (var comment in Model) { %>
    <li>
        <%= comment.Content %>
    </li>
<% } %>
</ul>

...

...
或者,这意味着我不需要将我的注释放在单独的用户控件中?我将如何传递问题的Id,以便只获取与该问题相关的注释?我认为最好的方法是为此使用单独的用户控件。看看我对RenderPartial的第一个评论。您可以使用say[question.comments]传入整个comments集合,也可以呈现部分控件并传入问题的id。实际上,我认为您需要将注释集合传递给控件,而不是问题id。因此,您需要将该模型从控制器传递给视图。您可以编写一个定制的模型绑定器,这非常简单,或者只需在控制器中创建一个类并将其传递回视图,然后针对每个问题将注释传递给部分控件。或者,这意味着我不需要将我的评论放在单独的用户控件中?我将如何传递问题的Id,以便只获取与该问题相关的评论?我认为最好的方法是使用单独的用户控件。看看我对RenderPartial的第一个评论。您可以使用say[question.comments]传入整个comments集合,也可以呈现部分控件并传入问题的id。实际上,我认为您需要将注释集合传递给控件,而不是问题id。因此,您需要将该模型从控制器传递给视图。您可以编写一个定制的模型活页夹,这很容易,或者简单地在控制器中创建一个类,并将其传递回视图,然后针对每个问题将注释传递给部分控件。+1类似于此,因为它重新体验了做事的视图更适合模式IMHO+1类似于此,因为它重新体验了做事的视图更适合模式IMHO