简体   繁体   English

如何从控制器传递列表以查看并显示为表

[英]How to pass a list from controller to view and display as table

I'm getting a error, but the error only says the title of my page; 我收到错误,但错误只显示了我的页面标题; when I try to debug and step through the code, it's going into the shared layout.cshtml page, but it steps all the way through, and fails on the footer (where there's only test and an image). 当我尝试调试并逐步执行代码时,它会进入共享的layout.cshtml页面,但它会一直步进,并在页脚上失败(只有测试和图像)。 I've tried commenting out the footer, and its elements, but that hasn't worked. 我试过评论页脚及其元素,但这没有奏效。 I can't seem to find anything either using the dev tools in chrome/firefox. 我似乎无法使用chrome / firefox中的dev工具找到任何东西。

Any hints as to where am I going wrong? 关于我哪里出错的任何提示?

This index.cshtml page originally pointed to a js file,where a layout is defined for the data. 此index.cshtml页面最初指向js文件,其中为数据定义了布局。 I left the layout there, but when it goes to the index, it then redirects it to the listing. 我将布局留在那里,但当它转到索引时,它会将其重定向到列表。 Could it be that the js file in the index is causing the error? 可能是索引中的js文件导致错误吗? I've tried commenting it out, but then the page is blank. 我已经尝试将其评论出来,但随后该页面一直是空白的。

The index.cshtml looks like this: index.cshtml看起来像这样:

@{
    ViewBag.Title = "People";
}

@section Head {
    @Html.ScriptFile("modules/people.js", false)
}

<div class="header"><span class="name">@ViewBag.Title</span></div>
<table class="data"></table>

The people.js file has this: people.js文件包含:

$(function () {
    if ($("#ID").length) {
        init();
} else {
    initializeTable("disclosures/listing", undefined, [
            { "sTitle": "ID" },
            { "sTitle": "Name" },
            { "sTitle": "Job Title" },
            { "sTitle": "Interview Complete?" },
            { "sTitle": "Completed On" },
            { "sTitle": "Last Update" }
        ], [
            { "name": "Completed: ", "column": 3 }
        ]);
      }
});

I've got an array of objects (essentially strings) in my Controller: 我的Controller中有一个对象数组(基本上是字符串):

List<PersonStatus> people = new List<PersonStatus>();

After adding items to the List, I pass it to my View: 将项目添加到列表后,我将其传递给我的视图:

return View("Listing", people);

and then display the array in the Listing View as a table: 然后以列表形式显示列表视图中的数组:

@model List<PersonStatus>
@{
    ViewBag.Title = "People";
}

@section Head {
    @Html.ScriptFile("modules/people.js", false)
}

<div class="header"><span class="name">@ViewBag.Title</span></div>
<div class="overflow"> 

@if (people == null)
{
    <table>
        <tr>
            <td>
                <i><font color="#cc0000">Information not available.</font></i>
            </td>
        </tr>
    </table>
}
@if (people != null)
{
    <table class="data">
        <thead>
            <tr>
                <th>IDNumber</th>
                <th>Name</th>
                <th>Job Title</th>
                <th>Interview Complete?</th>
                <th>Last Updated On</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var p in Model)
            {
               var person = p;

               <tr>
                <td>
                    <th>@people.IDNumber</th>
                    <th>@people.Name</th>
                    <th>@people.JTitle</th>
                    <th>@people.InterviewComplete</th>
                    <th>@people.LastUpdateDate</th>
                </td>
               </tr>
             }
        </tbody>
    </table>
}

If I understood you correctly, you may need to use foreach loop. 如果我理解正确,您可能需要使用foreach循环。 like: 喜欢:

@if (people != null)
{
    <table class="data">
        <thead>
               <tr>
                <th>IDNumber</th>
                <th>Name</th>
                <th>Job Title</th>
                <th>Interview Complete?</th>
                <th>Last Updated On</th>
               </tr>
          </thead>
        <tbody>
            foreach (var item in people)
            {
         <tr>
                <td>
                    <th>@item.IDNumber</th>
                    <th>@item.Name</th>
                    <th>@item.JTitle</th>
                    <th>@item.InterviewComplete</th>
                    <th>@item.LastUpdateDate</th>
                </td>
            </tr>
           }
        </tbody>
    </table>
}

You need to iterate through your collection using a foreach loop and create a new table row on each iteration. 您需要使用foreach循环遍历集合,并在每次迭代时创建一个新的表行。 Check out Foreach loop for tables in MVC4 查看MVC4中表的Foreach循环

OK,I think you are confused at System.NullReferenceException. 好的,我觉得你对System.NullReferenceException很困惑。 for your View Model you do like that 对于您的View Model,您确实喜欢这样

@model List<string>

and in your controller your code is 在您的控制器中,您的代码是

List<PersonStatus> people = new List<PersonStatus>(); return View(people);

the issue is you pass List<PersonStatus> to View but you define View Model as List<String> you should do like that 问题是你将List<PersonStatus>传递给View,但你将View Model定义为List<String>你应该这样做

@model List<PersonStatus>

please try this code: 请试试这段代码:

@model List<PersonStatus>
@{
    ViewBag.Title = "People";
}

@section Head {
    @Html.ScriptFile("modules/people.js", false)
}

<div class="header"><span class="name">@ViewBag.Title</span></div>
<div class="overflow"> 

@if (Model== null)
{
    <table>
        <tr>
            <td>
                <i><font color="#cc0000">Information not available.</font></i>
            </td>
        </tr>
    </table>
}
@if (Model!= null)
{
    <table class="data">
        <thead>
            <tr>
                <th>IDNumber</th>
                <th>Name</th>
                <th>Job Title</th>
                <th>Interview Complete?</th>
                <th>Last Updated On</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var p in Model)
            {
               <tr>
                <td>
                    <th>@p.IDNumber</th>
                    <th>@p.Name</th>
                    <th>@p.JTitle</th>
                    <th>@p.InterviewComplete</th>
                    <th>@p.LastUpdateDate</th>
                </td>
               </tr>
             }
        </tbody>
    </table>
}

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

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