MVC在每一行上运行代码/函数会产生LINQ查询

本文关键字:函数 代码 查询 LINQ 运行 一行 MVC | 更新日期: 2023-09-27 17:59:17

我对MVC(来自Webforms)非常陌生,我正在努力寻找如何用Webforms做一些简单的事情。

我使用LINQ Query从数据库中获得了一个数据(属性)列表,该数据显示在视图中,该数据的每一行都有一个唯一的id。

我试图实现两件事,我需要对每个属性运行一个函数并返回一个值,然后在视图中显示它。我还需要获得更多链接到该属性(唯一id)的数据(可能超过1行),然后在视图中迭代这些数据,所以我想我会嵌套foreach语句来在视图中显示该属性下的数据,但我不知道在哪里运行该查询以及如何在视图中进行显示。

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

Controller-获取属性列表

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; }
}

MVC在每一行上运行代码/函数会产生LINQ查询

您的视图中有这个

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

和类似下方CCD_ 2内部的CCD_

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

我认为这是在表中显示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>

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

public class CurrentViewModel
{
    ....
    .... your other properties
    ....
    public List<ViewingViewModel> ViewingViewModels { get; set; }
}

请记住,ASP中没有ItemDataBound事件。NET MVC类似于WebForms,因此获取相关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);        
}

最后在视图中添加嵌套的foreach0

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

始终确保右侧也返回List<ViewingViewModel>的实例。