简体   繁体   English

如何在ASP.NET MVC 4中显示从模型到另一个视图的列表

[英]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>


This is my Topic View: 这是我的主题视图:

 @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.

相关问题 ASP.NET MVC:如何将列表(从Model中的类)传递到View中的转发器? - ASP.NET MVC: How do I pass a list (from a class in Model) to a repeater in a View? 如何在ASP.NET MVC 3中为填充的下拉列表创建视图模型 - How do I create a view model for a populated drop down list in ASP.NET MVC 3 如何在ASP.NET MVC4中将ID从一个视图携带到另一个视图? - How do I carry an Id from one view to another, in ASP.NET MVC4? ASP.NET MVC,从视图获取模型列表值 - asp.NET mvc, get model list values from view C#-如何在ASP.NET MVC的HTML表单中传递内部带有其他模型的模型 - C# - How do I pass a model with another model inside from a HTML form in ASP.NET MVC 如何在ASP.Net MVC的视图内将项目添加到视图模型的列表中? - How can I add items to a list in my View Model within a View in ASP.Net MVC? ASP.NET MVC视图模型-如何在视图中显示数据? - ASP.NET MVC View Model - How do I display data in the View? 如何在 ASP.NET Core MVC“待办事项”应用程序中提取 URL 的文件名并在视图中显示给用户? - How do I extract the filename of a URL in a ASP.NET Core MVC "To Do" app and show it to the user in the View? 如何将带有自己的控制器的局部视图添加到 asp.net mvc 4 中的另一个视图 - How do I add a partial view with its own controller to another view in asp.net mvc 4 如何在 asp.net core 3.1 mvc 中的另一个表中显示类别名称而不是 categoryId? - How to show category name instead of categoryId in view from another table in asp.net core 3.1 mvc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM