繁体   English   中英

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

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

我收到错误,但错误只显示了我的页面标题; 当我尝试调试并逐步执行代码时,它会进入共享的layout.cshtml页面,但它会一直步进,并在页脚上失败(只有测试和图像)。 我试过评论页脚及其元素,但这没有奏效。 我似乎无法使用chrome / firefox中的dev工具找到任何东西。

关于我哪里出错的任何提示?

此index.cshtml页面最初指向js文件,其中为数据定义了布局。 我将布局留在那里,但当它转到索引时,它会将其重定向到列表。 可能是索引中的js文件导致错误吗? 我已经尝试将其评论出来,但随后该页面一直是空白的。

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>

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 }
        ]);
      }
});

我的Controller中有一个对象数组(基本上是字符串):

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

将项目添加到列表后,我将其传递给我的视图:

return View("Listing", people);

然后以列表形式显示列表视图中的数组:

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

如果我理解正确,您可能需要使用foreach循环。 喜欢:

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

您需要使用foreach循环遍历集合,并在每次迭代时创建一个新的表行。 查看MVC4中表的Foreach循环

好的,我觉得你对System.NullReferenceException很困惑。 对于您的View Model,您确实喜欢这样

@model List<string>

在您的控制器中,您的代码是

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

问题是你将List<PersonStatus>传递给View,但你将View Model定义为List<String>你应该这样做

@model List<PersonStatus>

请试试这段代码:

@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