부모와 자식보기간에 통신 할 수있는 HtmlHelper를 만들려고합니다. 나는 여기로부터 튜토리얼을 가지고있다 : https://gist.github.com/primaryobjects/8442193. 부모 뷰에서내 ViewContext.ViewBag를 내 코드와 함께 사용하려면 어떻게해야합니까?
@using SchoolIn.Helpers
및
@{Html.GetPageViewBag().PageTitle = "My Custom Property Readable By Parent View"; }
: 나는 사용하여 문을 추가 아이 뷰에서
namespace SchoolIn.Helpers
{
public static class ViewBagHelpers
{
public static dynamic GetPageViewBag(this HtmlHelper html)
{
if (html == null || html.ViewContext == null) //this means that the page is root or parial view
{
return html.ViewData;
}
ControllerBase controller = html.ViewContext.Controller;
while (controller.ControllerContext.IsChildAction) //traverse hierachy to get root controller
{
controller = controller.ControllerContext.ParentActionViewContext.Controller;
}
return controller.ViewBag;
}
}
}
:
는 우선이 같은 클래스와 메소드를 생성 나는 이것을하려고 노력하고있다 :
@{ ViewBag.Title = ViewContext.ViewBag.PageTitle }
그래서 나는 두 가지 문제가 있다고 생각합니다. 첫 번째는
ViewContex.ViewBag.
어떤 생각 나는이 일을 할 수있는 방법 ... 내가 튜토리얼처럼 내가 할 수없는 부모 뷰에서 html.ViewBag에 액세스 할 수 없습니다입니까? 어떤 도움을 주셔서 감사합니다.
입니다 보기? 이 작업을 수행해야하거나 레이아웃 /보기/부분/모델을 설계하는 더 좋은 방법이있을 수 있습니까? – Dangerous