单表包含来自多个数据库表的数据

时间:2017-05-13 09:33:15

标签: c# html asp.net linq razor

在我的dotnet Core项目中,我有3个数据库表:Building,Checklist和answer。

对于每个建筑物,我想根据清单和答案表创建一个包含信息的表格。我试图像下面的代码一样设置它,但它不起作用。

我的viewModel:

public class myViewModel
{
    public int id { get; set; }
    public List<Answers> myAnswers { get; set; }
    public List<Building> myBuildings { get; set; }
    public List<Check> myCheckpoints { get; set; }
}

我的控制器看起来像这样:

    public IActionResult viewData()
    {
        var currentUser = this.User.FindFirstValue(ClaimTypes.NameIdentifier);

        var vm = new myViewModel();
        vm.myBuildings = (from mb in db.buildings where mb.OwnerId == currentUser select mb).ToList();
        vm.myAnswers = (from o in db.answers select o).ToList();
        vm.myCheckpoints = (from mc in db.checks select mc).ToList();

        return View(vm);
    }

我的观点:

@model List<Server.myViewModel>

@foreach (var building in Model)
{
    <h4>@Html.DisplayFor(modelItem => building.myBuildings.buildingName)</h4>
    <table>
        <thead>
            <tr>
                <th>Avviksbeskrivelse</th>
                <th>Risiko</th>
                <th>Dato registrert</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>@Html.DisplayFor(modelItem => item.myCheckpoints.Description)</td>
                    <td>@Html.DisplayFor(modelItem => item.myAnswers.Deviation)</td>
                    <td>@Html.DisplayFor(modelItem => item.myAnswers.Risk)</td>
                </tr>
            }
        </tbody>
    </table>
}

使用@foreach时,我收到以下exeption消息:

  

System.InvalidOperationException:传递给的模型项   ViewDataDictionary的类型为'Server.myViewModel',但是这个   ViewDataDictionary实例需要类型的模型项   'System.Collections.Generic.IEnumerable`1 [Server.myViewModel]'。

我也无法从item.myAnswer转到item.myAnswer.Risk。

有谁知道如何解决这些问题,还是有更好的方法来解决这个问题?

1 个答案:

答案 0 :(得分:1)

你应该传递 myViewModel 列表来查看:

<!Doctype html>
    <html>
       <head>
          <title>Dobble</title>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
          <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
       </head>

       <body>
           <div id="canvas"></div>
           <script type="text/javascript">
              var w = 640,
                  h = 480;

              var data = {
                 name : "root",
                 children : [
                  { name: '1', size: 100, label: 'someText' },
                  { name: '2', size: 85, label: 'someText' },
                  { name: '3', size: 70, label: 'someText' },
                  { name: '4', size: 55, label: 'someText' },
                  { name: '5', size: 40, label: 'someText' },
                  { name: '6', size: 25, label: 'someText' },
                  { name: '7', size: 10, label: 'someText' },
                 ]
              }

              var canvas = d3.select("#canvas")
                 .append("svg:svg")
                 .attr('width', w)
                 .attr('height', h);

              var nodes = d3.layout.pack()
                 .value(function(d) { return d.size; })
                 .size([w, h])
                 .nodes(data);

              nodes.shift();

              canvas.selectAll('circles')
                  .data(nodes)
                 .enter().append('svg:circle')
                  .attr('cx', function(d) { return d.x; })
                  .attr('cy', function(d) { return d.y; })
                  .attr('r', function(d) { return d.r; })
                  .attr('fill', 'white')
                  .attr('stroke', 'grey');

           </script>
       </body>
    </html>