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