How to register javascript in ASP.NET MVC action?


How to register javascript in ASP.NET MVC action?



For example, I want to show a jQuery dialog after insert or edit a record in Action, in webform I can use the follow way to register js script to the page:

page.RegisterStartupScript(key, "<script language='javascript' defer>" + script + "</script>"); 

but this doesn't work in MVC, so how can I do this?

thanks!




When should functionality be made available in its own Controller?

1:



Why can't I reference JavaScript files in my View?
In ASP.NET MVC scripts are not registered in controller actions.


How to construct HttpPostedFileBase?
There's a separation between controllers and views.


How to resolve issue with image path when testing HtmlHelper?
So you could directly put the script in the corresponding view:.
Is a HTML helper a place for Business Logic?
<script type="text/javascript" src="somescript.js"></script> 
When the view is rendered it will load the script.


Is this code business logic or presentation logic?
Now if you are doing AJAX and you want to add a script inside the success callback you could create the script element and append it to the DOM:.
Sending collection of items in ASP.NET MVC
var script = document.createElement('script'); script.type = 'text/javascript'; script.src = 'somescript.js'; $('#someElement').append(script); 


ASP.NET MVC forces an AJAX request be redirected to the login page when the FormsLogin session is no longer active


87 out of 100 based on 57 user ratings 807 reviews