【问题标题】:Making HTML Tables in MVC4 using Strongly typed models使用强类型模型在 MVC4 中制作 HTML 表格
【发布时间】:2013-11-06 21:45:31
【问题描述】:

我正在尝试使用从数据库中提取的数据制作一个 HTML 表...

这是我的模型...

public class PopulationModels
{
    public List<PopulationModel> Populations { set; get; }
}

这只是一个...的列表

public class PopulationModel
{
    public string populationID { set; get; }

    public string PopName { set; get; }

    public string Description { set; get; }

    public string PopulationType { set; get; }

    public Boolean isActive { set; get; }

    //public List<XSLTACOData> patients { set; get; }
}

我像这样将这个模型传递给我的视图......

IEnumerable<PopulationModels> PopModel = DataRepo.Get(userName);
return View(PopModel);

我尝试通过以下方式显示列表...

@foreach (var item in Model) {
<tr>
    <td class="rowControl hidden">
        @*<a href="#makeRowEditable" class="makeRowEditable">Edit</a>*@ |
        @*<a href="#deleteRow" class="deleteRow">Delete</a>*@
        @Html.DispalyFor(modelItem => item.Populations)
    </td>
</tr>
}

但我很难弄清楚如何访问我的模型的各个元素...

例如

@Html.DispalyFor(modelItem => item.Populations)

上面的代码行,我想获取每个单独的 populationModel 并为每个字段映射一列...但我似乎无法访问各个人口...我在这里错过了一个合乎逻辑的步骤吗(我显然是,但哪一步?)

更新:添加更多视图:我现在正在制作一张桌子,但我这样做的方式似乎违反直觉......

这是我的模型...

 @model IEnumerable<FocusedReadMissionsRedux.Models.PopulationModels>

以下是我创建表格的方式(我计划在获得可靠的数据访问方式后立即使用 jqGrid 的 tableToGrid 功能...

<table border="2">
<thead>
  <tr>
  <th>
    Population Name
  </th>
   <th>
    Population Description
  </th>
  </tr>
</thead>
@foreach(var item in Model){
foreach (var pop in item.Populations)
{
<tr>
    <td class="rowControl hidden">
        @Html.DisplayFor(modelItem => pop.PopName)
        @Html.ActionLink(pop.PopName,"","")
    </td>
    <td>
        @Html.DisplayFor(modelItem => pop.Description)
        @Html.Display(pop.Description)
    </td>
</tr>
}

}

【问题讨论】:

    标签: asp.net-mvc-4 html-table


    【解决方案1】:

    由于您没有发布视图的完整代码:您应该像这样在视图中定义要绑定的对象类型:

    @model IEnumerable<PopulationModels>
    

    然后您可以使用 foreach 循环遍历它并使用以下方法绑定到当前项目:

    @Html.DisplayFor(x => x.Popname)
    

    【讨论】:

    • 我尽量不要发布太多代码。我不想放弃这里的农场。但我会用更多的东西更新我的 cmets。我似乎已经创建了表格,但我这样做的方式似乎违反直觉......我想知道你是否可以看看我可能会出错的地方?
    【解决方案2】:

    首先.. 一个好的命名约定是重命名您的

    PopulationModels
    

    PopulationList
    

    这是为了让后面的其他程序员了解模型的用途。

    引入你的模型:

    @model Models.PopulationList
    

    然后,我们可以使用 foreach 或 for 循环遍历模型列表:

    <table>
        <thead>
            <tr>
                <th>Population Name</th>
                <th>Population Description</th>
            </tr>
        </thead>
        <tbody>
        @foreach(Population pop in PopulationList)
        {
           <tr>
              <td>@Html.DisplayFor(model => pop.PopName)</td>
              <td>@Html.DisplayFor(model => pop.PopDesc)</td>
           </tr>
        }
        </tbody>
     </table>
    

    您只需要一个 foreach 循环,因为您正在迭代模型列表中的模型。

    我希望这对将来的参考有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 2012-06-21
      • 2010-09-27
      • 2019-09-28
      • 1970-01-01
      相关资源
      最近更新 更多