Haseeb
Haseeb

Reputation: 99

Individually print two columns of a table - LINQ C#

I am making my very very first beginner ASP.NET MVC App. I have connected my App with SQL Server and I have also seeded the DB. Now I want to print the information from a single table in SQL Server. Table in SQL Server consists of only 2 columns, but that is not my concern. I want print that data from SQL Server Table into my bootstrap table.

Image 1 shows how I am passing data to the View(). Image 2 shows a bootstrap table. Image 3 show how I want my data to be laid out. 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.

Image 1

Image 2

Image 3

Image 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.

Upvotes: 1

Views: 201

Answers (2)

Gaurav
Gaurav

Reputation: 162

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:

        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.

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>

Upvotes: 1

Alvin Almodal
Alvin Almodal

Reputation: 106

You need to use List in your controller and index view.

On your controller it should be something like this

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 List<TestModelTransfer.Models.JobCardEmployeeViewModel>

Once you do this you can now reference the model as follows:

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

Upvotes: 1

Related Questions