데이터베이스 우선 접근 방식으로 응용 프로그램에 두 개의 컨트롤러가 있습니다.
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
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; }
}
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());
}
}
SamCutReqMaster (상위)의 관점에서 SamcutAssignmentMaster (하위 클래스)의 "receivedBy"를 얻고 싶습니다. 그러나 SamCutReqMaster (FK는 SamCutReqID)와 관련된 SamcutAssignmentMaster에 데이터가 없을 수 있습니다.
아래 색인보기에서 SamcutAssignmentMaster.receivedBy에 액세스해야합니다.
@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>
확인. 유형이 인터페이스 ICollection 인 동안에는 속성에 액세스 할 수 없습니다. 인덱스를 통해 컬렉션의 속성에 액세스하려면 먼저이를 구체적인 유형으로 캐스팅해야합니다.
@for (var adCounter = 0; adCounter <= (Model.SamCutAssignmentMasters.Count - 1); adCounter++)
{
@Html.DisplayFor(x => ((List<SamCutAssignmentMaster>)x.SamCutAssignmentMasters)[adCounter].ReceivedBy)
}
위와 같이 유형을 목록으로 캐스팅하면 속성이 표시되어야합니다.
참고 :보기에서 DisplayFor를 사용하고 있습니다. 이 뷰의 내용을 게시하려는 경우 각 항목에 대한 숨겨진 컨트롤이 있거나 TextBoxFor로 전환하지 않는 한 모델은 바인딩되지 않습니다. 컬렉션의 항목에 바인딩하려면 for (Counter) 구문도 필요합니다.
도움이되기를 바랍니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다