简体   繁体   中英

Displaying a list into a Razor view

My goal is to combine two table and display them in a list on a razor view. With some help I was able to construct a linq query that pulls the data, as well as a viewModel.

I have the linq query here

public IActionResult AvailableJobIndex()
    {
        var today = DateTime.Now.Date;
        
        var jobs =
           from h in _context.PostThrs
           join e in _context.PostEigs on h.ThrZero equals e.EigZero
           where h.ThrDate > today && h.ThrText == "SERVICE DATE"
              && e.EigAgen == "OPEN"
           select new AgentClientIndexVM
           {
               Zero = h.ThrZero,
               ThrDate = h.ThrDate,
               ThrTime = h.ThrTime,
               ThrText = h.ThrText,
               EigAgen = e.EigAgen,
               EigRole = e.EigRole,
               EigLoad = e.EigLoad,
               EigNote = e.EigNote
           };


        return View(jobs.ToList());
    }

Below is the viewmodel that I constructed.

public class AgentClientIndexVM
{
    public string Zero { get; set; }

    public DateTime ThrDate { get; set; }

    public string ThrTime { get; set; }

    public string ThrText { get; set; }

    public string EigAgen { get; set; }

    public string EigRole { get; set; }

    public decimal EigLoad { get; set; }

    public string EigNote { get; set; }
}

I haven't put together a working view. My problem comes when I try to use a foreach statement. The viewmodel doesn't carry the list over. Please advise as to how to display the list in the view. Thanks

update:

this is my view. I cut it down with "..."

@model AURA.ViewModels.AgentClientIndexVM 

@{
    ViewData["Title"] = "AvailableJobIndex";
}

<h1>Available Job Index</h1>

<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Zero)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ThrDate)
            </th>
            ...
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayNameFor(model => model.Zero)
                </td>
                <td>
                    @Html.DisplayNameFor(model => model.ThrDate)
                </td>
                ...
                <td>
                    <a asp-action="PostDetail" asp-route-zero="@item.Zero">Details</a>
                </td>
            </tr>
        }
    </tbody>
</table>

update added another vm

public class AvailableJobListVM
{
    public List<AgentClientIndexVM> JobList { get; set; }
}

If you create another model and create a property of type List<AgentClientIndexVM> then you can enumerate as you are trying.

public class MyPageModel {
    public List<AgentClientIndexVM> MyList {get; set;}
}

MyPageModel model = new List<AgentClientIndexVM>();
model.MyList = from h in _context.PostThrs
               join e in _context.PostEigs on h.ThrZero equals e.EigZero
               where h.ThrDate > today && h.ThrText == "SERVICE DATE" && e.EigAgen == "OPEN"
               select new AgentClientIndexVM
               {
                   Zero = h.ThrZero,
                   ThrDate = h.ThrDate,
                   ThrTime = h.ThrTime,
                   ThrText = h.ThrText,
                   EigAgen = e.EigAgen,
                   EigRole = e.EigRole,
                   EigLoad = e.EigLoad,
                   EigNote = e.EigNote
               }.ToList();

        return View(model);

@model AURA.ViewModels.MyPageModel

Now you can use foreach

foreach(var element in Model.MyList) {
    statements
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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