简体   繁体   English

.NET MVC视图返回对象名称而不是特定对象

[英].NET MVC view returning object name rather than specific object

I'm returning a list of lists to an MVC view, displayed as a set of tables. 我将列表列表返回到MVC视图,显示为一组表。 I'm using nested for loops to try to return the values within each list. 我使用嵌套的for循环尝试返回每个列表中的值。

The tables are returning to the view with the expected number of rows in each, but instead of values in each of the rows, they are returning the names of their parameters, rather than the actual value of each parameter. 这些表将返回到具有每个行的预期行数的视图,但是这些表将返回其参数的名称,而不是每个参数的实际值,而不是每个行中的值。

The code in my view is currently: 我认为当前的代码是:

@if (Model.Report != null)
        {
            for(var row = 0; row < Model.Report.Count(); row++)
            {
                <table>
                    <tr>
                        <td><h4>@Html.DisplayNameFor(m=>m.Report[row].ReportData[row].ReportingGroup)</h4></td>
                    </tr>
                    <tr class="table-header">
                        <td style="text-align: center">Name</td>
                        <td style="text-align: center">Area</td>
                        <td style="text-align: center">Message</td>
                    </tr>

                    @for (var listing = 0; listing < Model.Report[row].ReportData.Count(); listing++)
                    {
                        <tr>
                            <td style="text-align: center">@Html.LabelFor(a=>a.Report[row].ReportData[listing].Name)</td>
                            <td style="text-align: center">@Html.LabelFor(a => a.Report[row].ReportData[listing].Area)</td>
                            <td style="text-align: center">@Html.LabelFor(a => a.Report[row].ReportData[listing].Message)</td>

                        </tr>
                    }
                </table>
            }
       }

Rather than the actual values of the parameters returning to each row in the view, I'm returning the parameter names("Name", "Area", and "Message") to each. 我将返回参数名称(“名称”,“区域”和“消息”),而不是返回视图中每一行的参数实际值。 I am wondering where I've gone wrong here. 我想知道我哪里出错了。

LabelFor displays a label for the property you pass it. LabelFor为您传递的属性显示标签。 Without further attributes, that's just the property name. 如果没有其他属性,那只是属性名称。

You just want to print the value itself: 您只想打印值本身:

@Model.Report[row].ReportData[listing].Message

You should replace your for loop with a foreach so you don't need to do all that. 您应该用foreach替换for循环,因此您无需执行所有操作。

更改LabelForDisplayForTextBoxFor在for循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM