Using jquery with asp.net mvc

Source: Internet
Author: User
Keywords Jque we we can.
Tags .net ajax asp asp.net beginning class codeplex create

by ASP.net MVC's built-in extensibility, developers can use third party libraries, such as jquery. When using ASP.net webforms, it is more difficult to use jquery instead of asp.net AJAX.

At the beginning of this article, the version of ASP.net mvc was preview 4, and some of the techniques used in preview 4 might not work in earlier versions. Preview 4 can be downloaded from the CodePlex.

Preliminary configuration

I'm not going to write it as a complete jquery guide, but simply give you a few examples of using this JavaScript library with asp.net mvc. Chad Myers has an excellent guide, including how to get started.

First make sure you have asp.net mvc, so download the installation from CodePlex First (note: You need to run visual Studio 2008 to use the ASP.net MVC framework).

After the ASP.net MVC framework is installed, a new asp.net mvc Web creator project should have been created.

Next, download jquery, download the packed or minified version, and put it in the content directory of the project above.

Add a reference to the jquery file under the content directory.

Simple example

Ryan Lanciaux wrote an excellent article called the jquery and asp.net MVC framework, which lists many of the key factors in using jquery in the asp.net MVC framework. Ryan made a detailed exposition in the article:

The first thing to do is create a asp.net MVC (Preview 4) project, create a new view and a controller action under Home controller, and add the following lines to the view.

This is red text,this are blueand this is green

Right-click the Controllers folder, select "Add New Item", and then select the MVC controller class to complete the task. The next step is to create a controller Action:

Next, we need to create a controller Action that returns the color value from model. And we don't want to overload the page, we want to use Ajax. Luckily, we can do this with the Jsonresult type in the MVC framework.

Public Jsonresult rgbcolors () {Colors.rgb color = new Colors.rgb (); return Json (color);

Next, create a class to represent the colors in model:

Namespace colors{public class RGB {public string Red = "#FF0000 ″;  public string Green = "#00FF00 ″;  public string Blue = "#0000FF"; } }

The final step is to assemble everything together with some jquery code:

$ (document). Ready (function () {$.getjson ("/home/rgbcolors", {}, function (data) {$ (". Red"). CSS ("Color", data.)  Red); $ (". Blue"). CSS ("Color", data.  Blue); $ (". Green"). CSS ("Color", data.  Green); }); });

Ryan points to an important place:

Note that the jquery code is raised in our controller using the JSON method. If we overload the page, it gets the color value defined in model. No pain at all. It's simple, but it can be used on the web.

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.