【问题标题】:How to pass a list from controller to view and display as table如何从控制器传递列表以查看并显示为表格
【发布时间】:2014-03-20 15:32:21
【问题描述】:

我收到一个错误,但该错误仅显示我的页面标题;当我尝试调试并单步执行代码时,它会进入共享的 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 }
        ]);
      }
});

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

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

【问题讨论】:

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


    【解决方案1】:

    如果我理解正确,您可能需要使用 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>
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我已经用我尝试过的方法更新了代码,但我得到了一个空引用;我认为我的问题更多是我没有将数据传递给视图?
      【解决方案3】:

      好的,我认为您对 System.NullReferenceException 感到困惑。 对于您的视图模型,您会这样做

      @model List&lt;string&gt;

      在你的控制器中你的代码是

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

      问题是您将 List&lt;PersonStatus&gt; 传递给 View,但您将 View Model 定义为 List&lt;String&gt; 你应该这样做

      @model List&lt;PersonStatus&gt;

      请试试这个代码:

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

      【讨论】:

      • 嗨@Rick,非常感谢您的建议。我已经尝试过了,如果我在 chrome/firefox 中使用开发人员工具,我会看到数据转到模型,但是我收到错误,并且数据不会显示在页面上。我已经根据您的建议/我的更改更新了我上面的帖子,您能否建议我尝试其他任何内容?
      猜你喜欢
      • 2021-06-19
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多