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,
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)