简体   繁体   中英

.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. I'm using nested for loops to try to return the values within each list.

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

更改LabelForDisplayForTextBoxFor在for循环。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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