4

부분보기를 사용하고/editortemplates 및 Kendo UI를 사용하는 asp.net mvc 4 응용 프로그램을 빌드하려고합니다. 나는 특정 페이지 뷰 모델이 있습니다 자신의 속성이 버그로displaytemplate에서 부모 viewmodel의 속성에 액세스하려고 시도합니다.

public class Plant { 
    public Guid PlantId{ get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public ICollection<Leaf> Leafs{ get; set; } 
    public ICollection<Flower> Flowers{ get; set; } 
    public ICollection<Bug> Bugs{ get; set; } 
} 

뿐만 아니라 잎, 꽃. 예를함으로써 는 : 그것이 더 쉽게 아약스로 업데이트 할 수 있도록

public class Leaf { 
    public Guid LeafId{ get; set; } 
    public string Name { get; set; } 
    public string Documentation { get; set; } 
    public string Description { get; set; } 
} 

내보기에 partialviews의 사용을 만들고있어. 내 기본보기 : PlantDetail.cshtml

내 부분보기 "_leafs"에서 (뿐만 아니라 "_flowers는"나는 LeafId과 PlantId을 필요로하는 작업을 호출 버튼의 세리의
@model Plant 

<table> 
<tr> 
    <td> 
     <h2>@Html.Label(Resources.PlantDetailTitle)</h2> 
    </td> 
    <td> 
     @Html.HiddenFor(m => m.PlantId) 
     @Html.DisplayFor(m => m.Name) 
    </td> 
</tr> 
<tr> 
    <td>@Html.LabelFor(m => m.Description) 
    </td> 
    <td> 
     @Html.DisplayFor(m => m.Description) 
    </td> 
</tr> 
</table> 

@{Html.RenderPartial("_flowers", Model);} 

@{Html.RenderPartial("_leafs", Model);} 

:

부분보기 "_leafs"

@model List<Leaf> 

@for (int i = 0; i < Model.Count(); i++) 
    { 
    @(Html.DisplayFor(m => m[i])) 
} 

내 displayTemplate "Leaf.cshtml"

@model Leaf 
    @Html.HiddenFor(m =>m.LeafId) 
    <a class='k-button k-button-icontext' [email protected]("InitiateLeaf", "Plant") +"?  
    [email protected]&plantId=#=PlantId#">@Model.Name</a> 
,

내 문제는 내 displaytemplate에서 부모 Viewmodel의 PlantId에 액세스 할 수없는 것입니다. (그리고 나는 내 displaytemplates 각각에서 같은 문제가있다.) 나는 allready 내 url.action에 routevalues와 그것을 시도하고 난 결국 자바 스크립트에서 PlantId에 액세스 할 수 있지만 모든 (mvc) 방법입니다 displaytemplates를 계속 사용하고 제 PlantI를 내 자녀 Leaf viewmodel의 속성으로 복제하지 않습니까?

나는 allready 내 displaytemplate에서 같은 "@ HttpContext.Current.Request.RequestContext.RouteData.Values ​​["컨트롤러 "]. ToString()"내 parentviewcontext을 액세서 것을 시도했다, 그러나 보이지 않는 내 PlantId의 가치를 찾으십시오 (심지어 거기에 저장되어있는 경우).

다른 사람에게도 여전히 제안이 있습니까?

+0

해결책을 찾았습니까? –

답변

1

언급 한 옵션 중 하나는 jquery를 사용하여 상위 PlantId에 액세스하는 것입니다. 부모 속성에 액세스해야하는 경우 Entity 프레임 워크에서 모델 클래스를 만드는 방법과 비슷한 뷰 모델을 디자인하는 것이 좋습니다.

따라서 리프 클래스에는 식물로 다시 돌아 가기위한 탐색 속성이 있어야합니다.

public class Leaf { 
    public Guid LeafId{ get; set; } 
    public string Name { get; set; } 
    public string Documentation { get; set; } 
    public string Description { get; set; } 
    public virtual Plant Plant { get; set; } 
    public Guid PlantId { get; set; } 
} 

ViewModel을 만들 때 Plant 속성과 PlantId를 채워야합니다. 희망이 도움이

관련 문제