首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC在每行结果LINQ查询上运行代码/函数

MVC在每行结果LINQ查询上运行代码/函数
EN

Stack Overflow用户
提问于 2016-07-29 15:43:57
回答 1查看 667关注 0票数 0

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

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

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

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

控制器-获取属性列表

代码语言:javascript
复制
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);
}

模型

代码语言:javascript
复制
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; }
}

视图

代码语言:javascript
复制
@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>

每个属性的视图模型

代码语言:javascript
复制
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; }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-29 17:12:04

你的看法是这样的

代码语言:javascript
复制
@model IEnumerable<PGS.Models.ViewModels.CurrentViewModel>

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

代码语言:javascript
复制
<tbody>
    @foreach (var item in Model)
    {
        <tr>
            ...
            ...
        </tr>
    }
</tbody>

我认为这是在表中显示CurrentViewModel列表的一个良好开端。如果我正确地理解了您的问题,看起来CurrentViewModel的一个实例可以有多个ViewingViewModel实例,并且您希望下面的结果如下所示:

代码语言:javascript
复制
<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为类型的新属性。

代码语言:javascript
复制
public class CurrentViewModel
{
    ....
    .... your other properties
    ....

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

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

代码语言:javascript
复制
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

代码语言:javascript
复制
<tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @foreach (var detail in item.ViewingViewModels)
                {
                    .... display ViewingViewModels here
                }
            </td>
            ...
            ...
        </tr>
    }
</tbody>

更新

根据您的注释,并假设ViewingViewModel的所有属性都与具有相同名称的SaleViewings属性相对应,控制器代码应该如下所示

代码语言:javascript
复制
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>,所以当您执行以下操作时

代码语言:javascript
复制
cvm.ViewingViewModels = ....

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38662736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档