2016-07-29 20 views
0

私はMVC(Webformsから来たもの)が非常に新しいので、Webformsで単純な方法を見つけるのに苦労しています。各行のMVC実行コード/機能結果LINQクエリ

私はLINQクエリを使用してデータベースからのデータ(プロパティ)のリストを持っており、このデータはビューに表示されており、このデータの各行には一意のIDがあります。

私が達成しようとしていることは2つあります。それぞれのプロパティでfuntionを実行して値を返し、ビューに表示する必要があります。 また、そのプロパティ(一意のID)にリンクされているデータを(1行以上)取得する必要がありますし、これらのビューを反復処理する必要があるため、プロパティの下にデータを表示するためにforeachステートメントをネストしますビューでは、しかし、私はちょうどこのクエリを実行する方法とビューで表示する方法がありません。

webformsでは、ItemDataBoundイベント(リピーターを使用している場合)でItemDataBoundで関数を呼び出したり、より多くのデータを要求したり、ネストされたリピーターに表示したりすることができます。

コントローラー - 各プロパティ

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; } 
} 
のために視聴するためのプロパティ

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> 

モデルのリストを取得します。

+1

試したサンプルデータと関連コードをご記入ください – Shyju

+0

現在、ありがとうございます。 – JBoom

+1

コード全体を投稿する必要はありません。関連するコードだけを投稿してください(あなたが求めている質問に対して)。どのプロパティをどのような方法で実行したいと思いますか?あなたがその方法を実行するのを止めているのは何ですか? – Shyju

答えて

1

あなたはあなたのビューで

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

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

以下のような<tbody>内部foreachループがこれを持って、私はそれが表に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

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

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

の種類としてList<ViewingViewModel>とそこにASP.NET MVCにはItemDataBoundイベントがWebフォームでのようなものだしないので、サブクエリが関連ViewingViewModelsがメインクエリの後、コントローラで行う必要があります取得することに注意してくださいの前。以下は、変更されたコントローラコード

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

、最終的には

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

UPDATEあなたのコメントに基づいて

とプロパティにViewingViewModel対応のすべてのプロパティを想定し、あなたのビューで、ネストされたforeachを追加しています同じ名前の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);   
} 

0はcvm.ViewingViewModelsのタイプはList<ViewingViewModel>であることを忘れないでください、あなたは常に

cvm.ViewingViewModels = .... 

ような何かをしたときに右側にもList<ViewingViewModel>のインスタンスを返すことを確認してください。

+0

ありがとうございます。今質問にエラーがあります。 context.SaleViewingsで 'foreach(var cvm in CaseList){cvm.ViewingViewModels = svから}を追加しました。ここで、sv.CaseID == cvm.CaseID select sv; } 'が見つかりましたが、WHEREでエラーが発生しました: '型' System.Linq.IQueryable 'を' System.Collections.Generic.List 'に暗黙的に変換できません。明示的な変換が存在します(キャストがありませんか?) ' – JBoom

+0

エラーは自明です。右側のクエリが 'System.Linq.IQueryable 'の代わりに' List 'のインスタンスを返すようにする必要があります。更新された回答を参照してください。 – ekad

+0

今は意味があり、働いています。あなたの助けと忍耐をありがとう。 – JBoom

関連する問題