none
mvc layout RRS feed

  • Question

  • Can i override only some specific elements like footer of layout page for one particular view.

    i am using asp mvc core 2

    • Moved by CoolDadTx Friday, August 18, 2017 7:24 PM ASP.NET related
    Friday, August 18, 2017 4:07 PM

All replies

  • Can i override only some specific elements like footer of layout page for one particular view.

    i am using asp mvc core 2

    You can create an action filter to override Layout file, but if you want to remove it, you will have to create an empty layout file instead of assigning the Master property to null. Like this:

    public class OverrideLayoutFilter : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            var view = filterContext.Result as ViewResult;
            view.MasterName = "_LayoutEmpty";
            base.OnResultExecuting(filterContext);
        }
    }
    Controller:
    
    public class HomeController : Controller
    {
        [OverrideLayoutFilter]
        public ActionResult Index()
        {
            return View();
        }
    }

    Now your new layout file needs to be placed in SharedFolder and you only put the RenderBody function inside

    _LayoutEmpty.cshtml

    @RenderBody()
    Friday, August 18, 2017 4:09 PM
  • i am working on mvc core 2.

    its not working, it is showing render body can only called from a layout page error.

    i need to use the default layout page but only for a particular one vie i need to change footer.

    thanks

    Friday, August 18, 2017 4:55 PM
  • also, showing viewresult has no definition for mastername

    view.MasterName = "_LayoutEmpty";


    Friday, August 18, 2017 5:02 PM
  • Sorry, I looked wrong...@@

    Basically , you can't do it on MVC 2, Maybe you could wait for MVC 3 or 4.


    Friday, August 18, 2017 5:12 PM
  • core 2 is latest one, i think
    Friday, August 18, 2017 5:19 PM
  • yes it is, in my exp i think there is no way can override layout page for one particular view. It's limited. on MVC or MVC Core
    Friday, August 18, 2017 5:34 PM
  • Thanks alot Sir, for your precious time.

    Friday, August 18, 2017 5:55 PM
  • It's my pleasure. : )
    Friday, August 18, 2017 6:05 PM
  • Please post questions related to MVC in the ASP.NET forums.
    Friday, August 18, 2017 7:24 PM
  • Ok, I am new on this forums, 
    Saturday, August 19, 2017 2:14 AM