Add css, js or other content to a views head from partial views

前端 未结 2 1499
终归单人心
终归单人心 2021-01-22 04:17

I have found a few questions relating to this, but generally there are many different answers and they all seem very messy and complex.

If that is what needs to be done,

2条回答
  •  囚心锁ツ
    2021-01-22 05:17

    You can do this with sections. For example: I have more than two view which each other has same _Layout.My Index action in Company Controller has a sections as follow:

    @model Invoice.Model.HelperClasses.CompanyViewModel
    
    @{
       ViewBag.Title = "Companies";
       Layout = "~/Views/Shared/_Layout.cshtml";
    }
    @section usage{
    
    }
    @section other{
    
    
    
    }
    @section script
    {
      
    }
    

    and Display Action in Invoice controller has same sections but different css and js as follow:

    @model Invoice.Model.HelperClasses.InvoiceViewModel
    
    @{
      ViewBag.Title = "Index";
      Layout = "~/Views/Shared/_Layout.cshtml";
    }
    @section usage{
    @**@
    }
    @section other{
      
      
      
    }
    @section script
    {
      
      
      
    }
    

    and then you can use this section in _Layout but its required argument should be false. Look at:

    
    
    
    
      @RenderSection("usage", required: false)
    
      @RenderSection("other", required: false)
    
      @RenderSection("script", required: false)
    
    
    
    
    

提交回复
热议问题