Ajax submission on ASP. net mvc + JQuery master page, mvcjquery

Source: Internet
Author: User

Ajax submission on ASP. net mvc + JQuery master page, mvcjquery

<Pre name = "code" class = "javascript"> Ajax content:
$ (Document ). ready (function () {$ ("# submit "). click (function () {var name = $ ("# log "). val (); var pwd = $ ("# pwd "). val (); if (name. length = 0) {alert ("Enter the user name! ");} Else if (pwd. length = 0) {alert (" enter the password! ");} Else {$. post (".. /.. /Content/Handler. aspx ", {mName: name, mPwd: pwd}, function (data) {if (data =" right ") {window. location. href = "ManagerIndex. aspx "; // specify the page on which you want to switch after successful logon} else {alert (data );}});}});});});

Create a Handler. aspx page and write it in the background:
 
<Pre name = "code" class = "csharp"> protected void Page_Load (object sender, EventArgs e) {string name = Request. form ["mName"]. toString (); string pwd = Request. form ["mPwd"]. toString (); if (name = "hu") {Response. write ("right"); Response. end ();} else {Response. write ("incorrect user name or password! "); Response. End ();}}

Note the path of the master page. It is best to put the Handler. aspx page under the Contents directory.
 

How does aspnet mvc30 implement ajax? The best example is to add Code such as implementation.

Jquery:
$. Ajax ({
Type: 'post ',
Url: '/Controller name/ActionResult method ',
Data: {parameters to be passed },
DataType: "json ",
Success: function (msg ){
Msg is the data returned by ActionResult.
}
});
Controller background
Public ActionResult test (here is the parameter list, for example, the parameter passed by ajax is {id: 1}. we can write it as a string id here)
{
This id is equal to 1.
/* ---- Add a database here ----*/
Dictionary <string, string> dict = new Dictionary <string, string> ();
Dict. Add ("infoId", "1 ");
Dict. Add ("infoMessage", "added successfully! ");
Return this. Json (dict );
}
In this way, the data returned by ajax is in json format {infoId: 1, infoMessage: added successfully !} Such data

How does aspnet mvc implement ajax?

Jquery's ease of use
However, you can also use @ ajax. actionlink @ ajax. beginform

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.