c# - 如何从 Controller 传递列表以查看并显示为表格

标签 c# javascript json visual-studio-2010 asp.net-mvc-3

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

有什么关于我哪里出错的提示吗?

这个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>();

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

return View("Listing", people);

然后在 ListView 中将数组显示为表格:

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

关于c# - 如何从 Controller 传递列表以查看并显示为表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22537760/

相关文章:

json - 带有空字符串验证的 Play2.1 JSON 格式

c# - 使用 C# 在本地计算机中创建注册表值

c# - 使用枚举和 |作为字典键

javascript - neo4j - 自动完成 JavaScript

javascript - Safari 阻止弹出窗口

javascript - 对手动创建的 Google map 的 API 访问

json - 使用 mongoimport 解析 JSON 失败

c# - 单元测试 session 窗口在调试时关闭

c# - 我的加速度计代码有什么问题?

javascript - 将 +""添加到字符串后追加 "0"