Search code examples
javascriptasp.net-mvcrazorhtml-helpersections

How to share a JS script between multiple views?


I'm working with ASP.NET MVC 5

I have two views, Create and Edit, they both share some Javascript methods. I'd like to find a way to write these JS methods only once, without an ugly copy / paste from a view to another

Actually, a temporary solution may be to write these methods in a separated .js file, and include it in both views.
This works, but I'd like to use some Html helper provided by MVC like @Url.Content("") ; so I must use a .cshtml file.

Is there a way to do that ? I was thinking about @sections, but I'm not sure if it's a good idea...

Any help would be appreciated


Solution

  • What you mention about a separate script and @section is the solution. Otherwise write you own HtmlHelper extension.