ASP. NET MVC uses AJAX to register action JavaScript to the page and execute

Source: Internet
Author: User

I believe that when you do WebForm, you often encounter the background CS file in the page to modify the page display style, display (hidden), or pop-up box according to the data running results. At that time we will use the Scriptmanage or page to register a section of JS to achieve page loading shows the effect we need.

In MVC, the Scriptmanage, Page object allows us to easily register the script with the page, which can only be done by Ajax.

Prerequisite:

1. First you need to refer to the jquery package and Unobtrusive-ajax in the loaded page

<script type= "Text/javascript" src= "@Url. Content (" ~/scripts/jquery-1.5.1.min.js ")" >    </script> <script type= "Text/javascript" src= "@Url. Content (" ~/scripts/jquery.unobtrusive-ajax.js ")" >    </script >

2. Action return data in controller

The action returned by the contenttype needs to be application/x-javascript,text/javascript, because the browser can parse the application/x-javascript finally resolves to text/javascript.

The first method: Use the Content method to return Contentresult, create the contents result object by using a string and a content type, so be explicit when using the contenttype type

Using JavaScript to return Javascriptresult objects

ASP. NET MVC uses AJAX to register action JavaScript to the page and execute

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.