每行的MVC运行代码/函数都会产生LINQ查询

时间:2016-07-29 15:43:57

标签: c# asp.net-mvc linq-to-sql

我对MVC很新(来自Webforms),我很难找到如何使用Webforms做一些简单的事情。

我使用LINQ Query从数据库中获得了数据(属性)列表,这些数据显示在视图中,并且此数据的每一行都有唯一的ID。

我正在努力实现两件事,我需要在每个属性上运行一个函数并返回一个值,然后在视图中显示它。 我还需要获取链接到该属性的更多数据(可能超过1行)(唯一ID),然后在视图中迭代这些数据,所以我想我会嵌套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; }
}

1 个答案:

答案 0 :(得分:1)

你在视图中有这个

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

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

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

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

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

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

请记住,ASP.NET MVC中没有ItemDataBound事件,就像在WebForms中一样,因此获取相关ViewingViewModel的子查询必须在主查询之后和{之前}在控制器中完成{1}}。以下是更改的控制器代码

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 = .... 的实例。