简体   繁体   中英

How to load JavaScript conditionally

Is there any way to conditionally load JavaScript reference files in ASP.Net Razor View? There are 15 web pages in my project. They refer to common set of libraries say A, B, C, D. However, there is one page which require library E instead of B.

How can I write the tags so that it will load library E only for that particular page? And also it will NOT LOAD B for the same page?

If I load E and B both, then the page crashes.

You can set some flag, such as

public ActionResult Index()
{
    ViewBag.Script = "B";
    return View();
}

and on your view

if ((ViewBag.Script as string) == "B")
{
}
else
{
}

You can use @section, but then you couldn't include all scripts on the _Layout

在布局视图上渲染

视图部分

ASP.NET MVC 3: Layouts and Sections with Razor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM