asp.net-mvcrazorasp.net-mvc-3-areas

adding attributes in HTML tag of specific views only?


I am working on a MVC application with razor view engine.

HTML, Head and Body tags are placed in shared view _Layout.cshtml and other views are using this as layout.

While supporting application I am in need to add a custom attribute in some of pages (not in all pages). If I add attribute in layout it will appear in all pages. Can you pleased guide me how adding an attribute can be managed only in desired pages.

Below is my html tag with attribute:

<html ng-app="business_rules">

Solution

  • You can create a Layout where you need all these attributes and just refer this layout for desired pages using

    @{
       Layout = "Path/To/Layout.cshtml";
    }
    

    on the top of those pages.

    For rest of the pages, you will use the different layout without those attributes.

    You can define the Layout form the controller too. It can be done as below:

    public ActionResult Index()
    {
        MyMOdel model = new MyMOdel ();
        //TO DO:
        return View("Index", "_AdminLayout", model);
    }