부모 클래스에서 자식 클래스 속성 가져 오기

Sreenath Ganga

데이터베이스 우선 접근 방식으로 응용 프로그램에 두 개의 컨트롤러가 있습니다.

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>
바퀴 73

확인. 유형이 인터페이스 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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Python 3의 자식에서 부모 클래스의 속성 가져 오기

C # 자식 클래스에서 부모 클래스 속성을 가져 오는 방법

정적 자식 클래스에서 호출 부모 클래스 가져 오기

부모 클래스에서 자식 클래스 읽기 전용 속성 변경

부모 클래스에서 모든 자식 모델 가져 오기-Django

파생 (자식) 클래스에 의한 오버로딩이 가능한 기본 (부모) 클래스 메서드의 클래스 특성에 액세스

Java에서 부모 클래스의 정적 메서드에서 자식 클래스 가져오기

Python에서 클래스의 모든 속성 가져 오기

클래스에서 모든 정적 속성 가져오기

상속 클래스에서 자식 가져 오기

자식 클래스에서 속성 가져 오기-가능합니까?

VBA 클래스 모듈 : 클래스 내부의 개체에서 속성 가져 오기

부모 메서드에서 자식 클래스 이름 가져 오기

부모로부터 자식 클래스 이름 가져 오기

PHP 부모 클래스 차이에서 자식 클래스 이름 가져 오기

자식 클래스에서 부모 개인 또는 보호 값 가져 오기

Flutter : StatefulWidget 자식 클래스에서 부모로 데이터 가져 오기

상속 된 속성 메서드가 자식 클래스의 메서드 내부에 래핑 될 때 작동하도록 가져 오기

다단계 상속 클래스의 부모 부모 속성 가져 오기

Unity C# 상속은 자식 클래스에서 부모 클래스로 값을 가져온 다음 자식 클래스로 가져옵니다.

c # 부모 클래스 속성없이 자식 속성 만 가져옵니다.

기본 클래스의 생성자 내 동일한 하위 모듈에서 다른 자식 클래스 가져오기

부모 클래스에서 사용되는 자식 클래스의 속성 초기화

부모 생성자가 호출되기 전에 자식 클래스의 부모 클래스 속성을 덮어쓰는 방법은 무엇입니까?

Java 부모 생성자가 호출되기 전에 자식 클래스의 부모 클래스 속성을 덮어 쓰는 방법

부모 클래스에서 자식 클래스의 변수 값을 가져오는 방법

drools의 부모 클래스에서 자식 클래스 속성에 액세스

기본 클래스에서 내부 가져 오기 전용 속성 재정의

자바에서 부모 패키지에서 가져 오기 클래스

TOP 리스트

  1. 1

    JNDI를 사용하여 Spring Boot에서 다중 데이터 소스 구성

  2. 2

    std :: regex의 일관성없는 동작

  3. 3

    JSoup javax.net.ssl.SSLHandshakeException : <url>과 일치하는 주체 대체 DNS 이름이 없습니다.

  4. 4

    PrematureCloseException : 연결이 너무 일찍 닫혔습니다.

  5. 5

    Xcode10 유효성 검사 : 이미지에 투명성이 없지만 여전히 수락되지 않습니까?

  6. 6

    정점 셰이더에서 카메라에서 개체까지의 XY 거리

  7. 7

    Ionic 2 로더가 적시에 표시되지 않음

  8. 8

    Seaborn에서 축 제목 숨기기

  9. 9

    C #에서 'System.DBNull'형식의 개체를 'System.String'형식으로 캐스팅 할 수 없습니다.

  10. 10

    복사 / 붙여 넣기 비활성화

  11. 11

    ArrayBufferLike의 typescript 정의의 깊은 의미

  12. 12

    Google Play Console에서 '예기치 않은 오류가 발생했습니다. 나중에 다시 시도해주세요. (7100000)'오류를 수정하는 방법은 무엇입니까?

  13. 13

    Kubernetes Horizontal Pod Autoscaler (HPA) 테스트

  14. 14

    jfreecharts에서 x 및 y 축 선을 조정하는 방법

  15. 15

    PRNG 기간보다 순열이 더 많은 목록을 무작위로 섞는 방법은 무엇입니까?

  16. 16

    C # HttpWebRequest 기본 연결이 닫혔습니다. 전송시 예기치 않은 오류가 발생했습니다.

  17. 17

    다음 컨트롤이 추가되었지만 사용할 수 없습니다.

  18. 18

    잘못된 구성 개체입니다. Webpack이 Angular의 API 스키마와 일치하지 않는 구성 개체를 사용하여 초기화되었습니다.

  19. 19

    Android Kotlin은 다른 활동에서 함수를 호출합니다.

  20. 20

    R의 마침표와 숫자 사이에 문자열 삽입

  21. 21

    Assets의 BitmapFactory.decodeStream이 Android 7에서 null을 반환합니다.

뜨겁다태그

보관