简体   繁体   English

单独打印表格的两列 - LINQ C#

[英]Individually print two columns of a table - LINQ C#

I am making my very very first beginner ASP.NET MVC App.我正在制作我的第一个初学者 ASP.NET MVC 应用程序。 I have connected my App with SQL Server and I have also seeded the DB.我已将我的应用程序与 SQL 服务器连接起来,并且我还为数据库播种了种子。 Now I want to print the information from a single table in SQL Server.现在我想从 SQL 服务器中的单个表中打印信息。 Table in SQL Server consists of only 2 columns, but that is not my concern. SQL 服务器中的表仅包含 2 列,但这不是我关心的问题。 I want print that data from SQL Server Table into my bootstrap table.我想将 SQL 服务器表中的数据打印到我的引导表中。

Image 1 shows how I am passing data to the View().图 1显示了我如何将数据传递给 View()。 Image 2 shows a bootstrap table.图 2显示了一个引导表。 Image 3 show how I want my data to be laid out.图 3显示了我希望如何布置数据。 As you can see in Image 4 , I can print only one column with Job Card IDs, but I want to print both columns, Job Card IDs and Employee ID and shown in Image 3 .正如您在图 4中看到的那样,我只能打印带有工作卡 ID 的一列,但我想打印两列,即工作卡 ID 和员工 ID,如图3所示。

Image 1图 1

Image 2图 2

Image 3图 3

Image 4图 4

note ~ i have been told to attached screenshots instead of linking them but for some reason it is not working for me or maybe i am just new to this.注意〜我被告知附上屏幕截图而不是链接它们,但由于某种原因它对我不起作用,或者我可能只是新手。 apologizes for that inconvenience.为给您带来的不便深表歉意。

You need to use List in your controller and index view.您需要在 controller 和索引视图中使用 List。

On your controller it should be something like this在您的 controller 上应该是这样的

public ActionResult Index()
    {
        var jobCardEmployeeList = new List<JobCardEmployeeViewModel>();
        jobCardEmployeeList.Add(new JobCardEmployeeViewModel
        {
            Employee = new EmployeeModel { ID = 1 },
            JobCard = new JobCardsModel { ID = 2 }
        });
        return View(jobCardEmployeeList);
    }

And on your view page reference the model as a list as well.在您的查看页面上,还将 model 作为列表引用。

@model List<TestModelTransfer.Models.JobCardEmployeeViewModel>

Once you do this you can now reference the model as follows:完成此操作后,您现在可以参考 model,如下所示:

   <tbody>
    @foreach(var jobCardEmplomodelyee in Model)
    {
        <tr>
            <td>@jobCardEmplomodelyee.Employee.ID</td>
            <td>@jobCardEmplomodelyee.JobCard.ID</td>
        </tr>
    }
</tbody>

If you want to display records of only one sql table then fetch items of that table as a List and pass it to the view, something like this:如果您只想显示一个 sql 表的记录,则将该表的项目作为列表获取并将其传递给视图,如下所示:

        var records = _dbContext.table_name.ToList();

Or you can add a ViewModel of this table and map these table records into it making it a list.或者您可以添加该表的 ViewModel 和 map 这些表记录到其中,使其成为一个列表。

example:例子:

    public ActionResult Index()
    {
        // Get your data of your table from the sql table here
        // I have used a dummy list for demo
        var result = new List<EmployeeTableVM>() {
            new EmployeeTableVM(){
                EmployeeID = 1,
                JobCardID = 011
            },
            new EmployeeTableVM(){
                EmployeeID = 2,
                JobCardID = 012
            }
        };

        return View(result);
    }

Your view will be something like this:您的视图将是这样的:

@model IEnumerable<WebApplication2.Controllers.EmployeeTableVM>
<div>
<table class="table">
    <thead>
        <tr>
            <th scope="col">JobCard ID</th>
            <th scope="col">Employee ID</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.JobCardID</td>
                <th>@item.EmployeeID</th>
            </tr>
        }
    </tbody>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM