As follows:
1: Non-Ajax
Front-end:
Corresponding background:
2: Ajax of JQuery
Before using JQuery to submit a form, we need to first reference jquery. form. js, which is from http://www.malsup.com/jquery/form/. in this case, the js files to be referenced are:
Function requirements: Ajax submits a form, processes the request in the Controller HelloWorld4Controller, and returns some data in JSON format.
First, assume that the returned JSON object is:
Copy codeThe Code is as follows:
Public class LoginResultDTO
{
Public bool Success {get; set ;}
Public string Message {get; set ;}
Public string ReturnUrl {get; set ;}
}
The Controller code is as follows:
Copy codeThe Code is as follows:
Public class HelloWorld4Controller: Controller
{
Public ActionResult Index ()
{
If (Request. IsAjaxRequest ())
{
String str1 = Request. Form ["Text33"];
String str2 = Request. Form ["Text44"];
Return Json (new MvcApplication5.Controllers. HelloWorld3Controller. LoginResultDTO {Success = true, Message = str1 + str2, ReturnUrl = "SomeUrl "});
}
Else
{
Return View ();
}
}
}
The code above tells us that it is convenient to return other entities.
Front-end code:
If the button does not use submit, but a button, the code is changed to the following format:
Source code download: MvcApplication5.zip