1

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.

1 Answer 1

2

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

render on layout view

section on view

ASP.NET MVC 3: Layouts and Sections with Razor

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks BrunoLM! The first option looks easier. Let me give it a try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.