2017-05-17 12 views
0

私は親クラスの子クラスPropertを取得

データベースの最初のアプローチと私のアプリケーションでは二つのコントローラ

SampCutReqMaster.CS

public partial class SampCutReqMaster 
    { 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
     public SampCutReqMaster() 
     { 
      this.SamCutAssignmentMasters = new HashSet<SamCutAssignmentMaster>(); 
     } 

     public decimal SampCutreqID { get; set; } 
     public Nullable<decimal> BuyerID { get; set; } 
     public Nullable<decimal> PatternRefID { get; set; } 
     public Nullable<decimal> PatternStyleID { get; set; } 
     public Nullable<decimal> SampleTypeID { get; set; } 


     public virtual BuyerMaster BuyerMaster { get; set; } 
     public virtual PatternStyle PatternStyle { get; set; } 
     public virtual PatterRefMaster PatterRefMaster { get; set; } 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<SamCutAssignmentMaster> SamCutAssignmentMasters { get; set; } 
     public virtual SampleType SampleType { get; set; } 
    } 

と隣にありました

SamCutAssignmentMaster

私はSamCutReqMasterの私の見解(親)にSamcutAssignmentMasterの「receivedBy」(子クラス)を取得したい.ButそこかもしれSampCutReqMaster

public class SampCutReqMastersController : Controller 
    { 
     private ArtEntities db = new ArtEntities(); 
    // GET: SampCutReqMasters 
    public ActionResult Index() 
     { 
      var sampCutReqMasters = db.SampCutReqMasters.Include(s => s.BuyerMaster).Include(s => s.PatternStyle).Include(s => s.PatterRefMaster).Include(s => s.SampleType).Include(s=>s.SamCutAssignmentMasters); 

      var sampCutReqMasterssort = sampCutReqMasters.ToList().OrderByDescending(a => a.AddedDate); 
      return View(sampCutReqMasterssort.ToList()); 
     } 
} 

のインデックスビューとコントローラを作成していた

public partial class SamCutAssignmentMaster 
    { 
     public decimal CutAssignID { get; set; } 
     public decimal SampCutreqID { get; set; } 
     public System.DateTime ReceivedDate { get; set; } 
     public string ReceivedBy { get; set; } 
     public Nullable<decimal> PatternMasterID { get; set; }     

     public virtual PatternMaster PatternMaster { get; set; } 
     public virtual SampCutReqMaster SampCutReqMaster { get; set; } 
    } 

SamCutReqMaster(FKがSamCutReqIDある)インデックスビューで

私は

@model IEnumerable<WebArtSampler.Models.SampCutReqMaster> 

@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 
<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.ReqNum) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Fabric) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.SampleRequiredDate) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.AddedDate) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.AddedBy) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.BuyerMaster.BuyerName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.PatternStyle.StyleName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.PatterRefMaster.PatterRefNum) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.SampleType.SampleType1) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.SizeDetail) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Qty) 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.ReqNum) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Fabric) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.SampleRequiredDate) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.AddedDate) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.AddedBy) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.BuyerMaster.BuyerName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.PatternStyle.StyleName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.PatterRefMaster.PatterRefNum) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.SampleType.SampleType1) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.SizeDetail) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Qty) 
     </td> 

     <td> 
      @Html.ActionLink("Edit", "Edit", new { id=item.SampCutreqID }) | 
      @Html.ActionLink("Details", "Details", new { id=item.SampCutreqID }) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.SampCutreqID }) 
     </td> 
    </tr> 
} 

</table> 
SamcutAssignmentMaster.receivedByにアクセスする必要があります下にrelavant SamcutAssignmentMasterにはデータもありません
+0

実際の作業コードを表示してください。これは決してコンパイルされません –

+0

部分クラスは何ですか?私は知っているが、私はほとんどすべての仮想クラスのプロパティの値を取得することができます。それは、継承とはあまり関係がありません。 – Mardoxx

+0

@Mardoxx私は知っています。表示されるitem.PatterRefMaster.PatterRefNumのようなものです。 IcollectionにあるSamCutAssignmentMastersのプロパティにアクセスすることはできません。public ICollection SamCutAssignmentMasters {get;セット; } –

答えて

1

あなたのタイプはインターフェイスICollectionのままですが、プロパティにアクセスすることはできません。索引を使用してコレクションのプロパティにアクセスするには、そのコレクションを具体的な型にキャストする必要があります。

@for (var adCounter = 0; adCounter <= (Model.SamCutAssignmentMasters.Count - 1); adCounter++) 
{ 
    @Html.DisplayFor(x => ((List<SamCutAssignmentMaster>)x.SamCutAssignmentMasters)[adCounter].ReceivedBy) 
} 

上記のようにタイプをリストにキャストすると、そのプロパティが表示されます。

注:DisplayForをビューで使用しています。このビューの内容を投稿する予定がある場合は、各項目のコントロールが隠れていないか、TextBoxForに切り替えていない限り、モデルはバインドされません。コレクションの項目にバインドするにはfor(Counter)構文も必要です。

希望に役立ちます。

+0

ありがとう。私はそれを必要とするだけ表示目的のために私は転記されません。 @ Html.DisplayFor(x =>((List )item.SamCutAssignmentMasters)[i] .ReceivedBy)しかし、エラーが発生する追加情報:Unable to 'System.Collections.Generic.HashSet'1 [WebArtSampler.Models.SamCutAssignmentMaster]'タイプのキャストオブジェクトを入力して –

+0

@SreenathGangaを入力してください - HashSetの理由はありますか?これは、ICollectionに重複がないことを保証するためです。 SampCutReqMasterのコンストラクタを呼び出すときに、SamCutAssignmentMastersを新しいHashSetに設定します。強制する特別な制約がない場合は、リストを使用して始めるのがよいでしょうか? – Wheels73

関連する問題