문제

우연히 특정 컨트롤러 내부의 모든 작업에 대한 기본 마스터 뷰를 설정하는 쉬운 방법이 있습니까?

예를 들어 내가있는 경우 홈 컨트롤러 나는 그것의 내부의 모든 행동이 Site.master 기본적으로, 그러나 내가 안에있는 경우 AccountsController 나는 모든 행동이 상속을 받기를 원합니다 관리자 마스터 등등..

나는 그것을 가지고있다 :

return View("viewName", "masterName", objectModel);

그러나이 작업을 수행하면보기 방법을 호출 할 때마다 적용해야합니다.

나는 우리가 선언 할 수있는 레일과 같은 더 간단한 것을 찾고있었습니다.

class HomeController < ApplicationController

  layout 'site'

   def index
   end

   def create
   ...

end

class AccountsController < ApplicationController

  layout 'admin'

  def index
  end

  def create
  ...  

end

그게 가능합니까?

미리 감사드립니다

도움이 되었습니까?

해결책

해당 컨트롤러 클래스에서 OnActionExecuting을 무시할 수 있습니다.

protected override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
    ViewData["MasterfileToUser"] = "site";
}       

또는 좋아한다면 이것을 ActionFilterattribute로 바꿀 수 있습니다. 컨트롤러 또는 작업 레벨에 적용 할 수 있습니다.

using System;
using System.Web.Mvc;
public class MasterFileFilterAttribute : ActionFilterAttribute
{
    public string Master { get; set; }

    public override void OnActionExecuted( ActionExecutedContext filterContext)   
    {        
            if (filterContext.Result is ViewResult)
                    ((ViewResult)filterContext.Result).MasterName = Master;
    }
}

그런 다음 당신은 다음과 같이 사용합니다.

[MasterFileFilterAttribute(Master = "site")] 
public class HomeController : Controller 
{ 
    // Action methods 
}   
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top