ASP. net mvc ajax processing AjaxResult, mvcajaxresult
1. Define ajax Request Response formats in aspnet mvc to facilitate the frontend to process ajax results in a unified manner.
1) define the format of data returned by the program
1 /// <summary> 2 /// execution result 3 /// </summary> 4 /// <typeparam name = "T"> </typeparam> 5 public class runResult <T> 6 {7 // <summary> 8 // operation Status 9 /// </summary> 10 public ResultStatus {get; set;} 11 // <summary> 12 // operation Exception 13 /// </summary> 14 public Exception {get; set ;} 15 /// <summary> 16 // data Result 17 /// </summary> 18 public T Result {get; set ;} 19 /// <summary> 20 /// operation Message 21 /// </summary> 22 public string Message {get; set ;}23}View Code
2) define the execution result status
/// <Summary> /// execution result /// </summary> /// <typeparam name = "T"> </typeparam> public class RunResult <T> {/// <summary> /// operation Status /// </summary> public ResultStatus Status {get; set ;}//< summary> /// operation Exception /// </summary> public Exception {get; set ;} /// <summary> /// data Result /// </summary> public T Result {get; set ;} /// <summary> /// operation Message /// </summary> public string Message {get; set ;}}View Code
3) return the front-end data format
Public class AjaxEntity <T> {[JsonProperty (PropertyName = "Success")] public bool Success {get; set;} [JsonProperty (PropertyName = "Message")] public string Message {get; set;} [JsonProperty (PropertyName = "Data")] public T Data {get; set ;}}View Code
4) define ajax ActionResult in asp.net mvc
Public class AjaxResult <T>: CustomResult <AjaxEntity <T> {private readonly RunResult <T> _ runResult; public AjaxResult (RunResult <T> runResult) {_ runResult = runResult ;} /// <summary> ///// </summary> /// <returns> </returns> public override AjaxEntity <T> GetObject () {AjaxEntity <T> entity = new AjaxEntity <T> {Data = _ runResult. result}; if (_ runResult. result is bool) {entity. success = _ runResult. status = ResultStatus. success & Convert. toBoolean (_ runResult. result);} else {entity. success = _ runResult. status = ResultStatus. success;} entity. data = _ runResult. result; entity. message = _ runResult. message; return entity ;}}View Code
How does aspnet mvc implement ajax?
Jquery's ease of use
However, you can also use @ ajax. actionlink @ ajax. beginform
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