[英]How do i show a list from a model to another View in ASP.NET MVC 4
I have a list of Topic
's. 我有一个
Topic
列表。 Each Topic
have a list of Message
. 每个
Topic
都有一个Message
列表。 When i click on "Details" I want to open a new View that display a list of the Message
's from the Topic. 当我单击“详细信息”时,我想打开一个新视图,该视图显示主题中
Message
的列表。
I expect that I need to modify this line: (this is what I have so far) 我希望我需要修改以下行:(这是我到目前为止的内容)
@Html.ActionLink("Details", "Details", new { id=item.Id }, null)
The Message
's are accessed by item.Message
. Message
可以通过item.Message
访问。
And the first line in the Detail View 以及细节视图中的第一行
@model IEnumerable<Models.TopicMessage>
@model IEnumerable<Models.Topic> <table> <tr> <th> @Html.DisplayNameFor(model => model.Name) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.ActionLink("Details", "Details", new { id=item.Id }, null) </td> </tr> } </table>
There are a number of ways to do that. 有很多方法可以做到这一点。 You can use the
Action
method to execute the controller action directly: 您可以使用
Action
方法直接执行控制器操作:
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.Action("Details", "Details", new { id=item.Id })
</td>
</tr>
Or you can bypass the controller and render the view directly using this overload of DisplayFor
: 或者,您可以使用
DisplayFor
此重载绕过控制器并直接呈现视图:
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Messages, "MessagesDetail")
</td>
</tr>
Or if you define the template as the default DisplayTemplate
, you can just do this: 或者,如果您将模板定义为默认的
DisplayTemplate
,则可以执行以下操作:
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Messages)
</td>
</tr>
I fixed this by making a somethingViewModel
that contained both the liste and the Id. 我通过制作一个同时包含liste和Id的
somethingViewModel
来解决此问题。 That way i could contain the two information from two difference places in one new object I had access to in the View. 这样,我可以将来自两个不同位置的两个信息包含在我可以在视图中访问的一个新对象中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.