MVC 运行 code/function 每行结果 LINQ 查询

MVC run code/function on each row results LINQ Query

我是 MVC 的新手(来自 Webforms),我正在努力寻找如何使用 Webforms 做一些简单的事情。

我有一个使用 LINQ 查询从数据库中获取的数据(属性)列表,该数据显示在视图中,并且该数据的每一行都有一个唯一的 ID。

有两件事我想实现,我需要 运行 每个 属性 和 return 一个值的函数,然后在视图中显示它。 我还需要获取更多链接到 属性(唯一 ID)的数据(可能超过 1 行),然后在视图中遍历这些数据,所以我想我会嵌套 foreach 语句来显示数据在视图中的 属性 下,但我不知道在哪里 运行 这个查询以及如何在视图中显示。

使用网络表单,我可以很容易地在 ItemDataBound 事件中编写我想要的任何内容(如果使用转发器)、调用 ItemDataBound 中的函数或请求更多数据并在嵌套转发器中显示。

控制器 - 获取属性列表

public ActionResult Index()
{

    // list of pre-offer cases       
    IList<CurrentViewModel> CaseList = new List<CurrentViewModel>();
    var query = from cr in context.CaseRegs
                join c in context.PgsClients on cr.ClientID equals c.ClientID
                join s in context.PgsSites on cr.SiteID equals s.SiteID into scr from s in scr.DefaultIfEmpty()
                join b in context.Buyins on cr.CaseID equals b.CaseID into bcr from b in bcr.DefaultIfEmpty()
                where cr.arcStatus == "Current" && cr.withdrawn == null
                orderby cr.CaseID
                select new CurrentViewModel
                {
                    CaseID = cr.CaseID,
                    RegistrationDate = cr.RegistrationDate.GetValueOrDefault(),
                    ClientName = c.ClientName,
                    ClientCode = c.ClientCode,
                    SiteName = cr.SiteName,
                    SiteName2 = s.SiteName,
                    ClientPlot = cr.ClientPlot,
                    ContactName = cr.ContactName,
                    PxpStatus = cr.arcStatus,
                    CaseStatus = cr.caseStatus,
                    OwnerTitle = cr.ownerTitle,
                    OwnerFirstname = cr.ownerFirstname,
                    OwnerSurname = cr.ownerSurname,
                    PropertyAddress = cr.propertyAddress,
                    PropertyAddress2 = cr.propertyAddress2,
                    PropertyAddress3 = cr.propertyAddress3,
                    PropertyTown = cr.propertyTown,
                    PropertyCounty = cr.propertyCounty,
                    PropertyPostcode = cr.propertyPostcode,
                    TargetCompletionDate = b.TargetCompletionDate
                };

    CaseList = query.ToList();

    return View(CaseList);
}

型号

public class CurrentViewModel
{
    [Key]
    public int CaseID { get; set; }
    public DateTime RegistrationDate { get; set; }
    public int ClientID { get; set; }
    public string ClientName { get; set; }
    public string ClientCode { get; set; }
    public int SiteID { get; set; }
    public string SiteName { get; set; }
    public string SiteName2 { get; set; }
    public string ClientPlot { get; set; }
    public string ContactName { get; set; }
    public string PxpStatus { get; set; }
    public string CaseStatus { get; set; }
    public string OwnerTitle { get; set; }
    public string OwnerFirstname { get; set; }
    public string OwnerSurname { get; set; }
    public string PropertyAddress { get; set; }
    public string PropertyAddress2 { get; set; }
    public string PropertyAddress3 { get; set; }
    public string PropertyTown { get; set; }
    public string PropertyCounty { get; set; }
    public string PropertyPostcode { get; set; }
    [DataType(DataType.Date)]
    public DateTime? TargetCompletionDate { get; set; }
}

查看

@model IEnumerable<PGS.Models.ViewModels.CurrentViewModel>
@using PGS.Utilities

@{
    Layout = "~/Views/Shared/_Main_Template.cshtml";
}

<h1>@ViewBag.Title</h1>


<table class="table">
    <thead>
        <tr>
            <th colspan="7">
                <span class="record-count">@ViewBag.RecordCount</span>
            </th>
        </tr>
        <tr>
            <th>Case</th>
            <th>Start Date</th>
            <th>Target Completion</th>
            <th>Site/Plot</th>
            <th>Name</th>
            <th>Property</th>
            <th>Client</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {

            string RowClassName = "row-" + @item.CaseID;

            <tr class="row-click @RowClassName" data-url="/Case/Details/@item.CaseID/">
                <td>
                    @Html.DisplayFor(modelItem => item.CaseID)
                </td>
                <td>
                    @item.RegistrationDate.Date.ToString("dd-MMM-yyyy")
                </td>
                <td>
                    @if (item.TargetCompletionDate.HasValue)
                    { @item.TargetCompletionDate.Value.ToString("dd-MMM-yyyy") }
                </td>
                <td>
                    @Extensions.SitePlotFormat(item.ClientPlot, item.SiteName, item.SiteName2)
                </td>
                <td>
                    @Extensions.VendorNameFormat(item.OwnerTitle, item.OwnerFirstname, item.OwnerSurname)
                </td>
                <td>
                    @Extensions.PropertyShortAddressFormat(item.PropertyAddress, item.PropertyTown, item.PropertyCounty, item.PropertyPostcode)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ClientCode)
                </td>
            </tr>
        }
    </tbody>
</table>

每个 属性

的观看模型
public class ViewingViewModel
{
    [Key]
    public int ID { get; set; }
    public int CaseID { get; set; }
    public DateTime Date { get; set; }
    public string Applicant { get; set; }
    public int AgentID { get; set; }
}

你有这个在你看来

@model IEnumerable<PGS.Models.ViewModels.CurrentViewModel>

<tbody> 内的 foreach 循环,如下所示

<tbody>
    @foreach (var item in Model)
    {
        <tr>
            ...
            ...
        </tr>
    }
</tbody>

我认为这是在 table 中显示 CurrentViewModel 列表的良好开端。如果我正确理解你的问题,看起来 CurrentViewModel 的一个实例可以有多个 ViewingViewModel 的实例,你想要像下面这样的结果作为最终结果:

<tbody>
    <tr>
        <td>
            .... 
            .... display the instances of ViewingViewModel here
            ....
        </td>
        ....
        ....
    </tr>
    <tr>
        <td>
            .... 
            .... display the instances of ViewingViewModel here
            ....
        </td>
        ....
        ....
    </tr>
    ....
    ....
</tbody>

您需要做的第一件事是将类型为 List<ViewingViewModel> 的新 属性 添加到 CurrentViewModel

public class CurrentViewModel
{
    ....
    .... your other properties
    ....

    public List<ViewingViewModel> ViewingViewModels { get; set; }
}

请记住,在 ASP.NET MVC 中没有像 WebForms 中那样的 ItemDataBound 事件,因此获取相关 ViewingViewModels 的子查询必须在主查询之后和 return View(CaseList);。下面是更改后的控制器代码

public ActionResult Index()
{
    IList<CurrentViewModel> CaseList = new List<CurrentViewModel>();
    var query = .... // your existing query as in the question

    CaseList = query.ToList();

    foreach (var cvm in CaseList)
    {
        cvm.ViewingViewModels = .... // do the query to get the ViewingViewModels here
    }

    return View(CaseList);        
}

最后在您的视图中添加嵌套的 foreach

<tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @foreach (var detail in item.ViewingViewModels)
                {
                    .... display ViewingViewModels here
                }
            </td>
            ...
            ...
        </tr>
    }
</tbody>

更新

根据您的评论并假设 ViewingViewModel 的所有属性都对应于 SaleViewings 的同名属性,控制器代码应如下所示

public ActionResult Index()
{
    IList<CurrentViewModel> CaseList = new List<CurrentViewModel>();
    var query = .... // your existing query as in the question

    CaseList = query.ToList();

    foreach (var cvm in CaseList)
    {
        cvm.ViewingViewModels = (from sv in context.SaleViewings 
                                where sv.CaseID == cvm.CaseID 
                                select new ViewingViewModel
                                {
                                    ID = sv.ID,
                                    CaseID = sv.CaseID,
                                    Date = sv.Date,
                                    Applicant = sv.Applicant,
                                    AgentID = sv.AgentID
                                }).ToList();
    }

    return View(CaseList);        
}

记住cvm.ViewingViewModels的类型是List<ViewingViewModel>,所以当你做类似

的事情时
cvm.ViewingViewModels = ....

始终确保右侧也是 returns List<ViewingViewModel> 的实例。