ASP. NET MVC Ajax processing Ajaxresult

Source: Internet
Author: User
Tags format definition

1, unified ASPNET MVC to the AJAX Request response format definition, convenient front-end unified processing AJAX results.

1) Define program return result data format

1 /// <summary>2     ///Execution Results3     /// </summary>4     /// <typeparam name= "T" ></typeparam>5      Public classRunresult<t>6     {7         /// <summary>8         ///Operation Status9         /// </summary>Ten          PublicResultstatus Status {Get;Set; } One         /// <summary> A         ///Operation Exception -         /// </summary> -          PublicException Exception {Get;Set; } the         /// <summary> -         ///Data Results -         /// </summary> -          PublicT Result {Get;Set; } +         /// <summary> -         ///Action Messages +         /// </summary> A          Public stringMessage {Get;Set; } at}
View Code

2) Define execution result status

/// <summary>    ///Execution Results/// </summary>    /// <typeparam name= "T" ></typeparam>     Public classRunresult<t>    {        /// <summary>        ///Operation Status/// </summary>         PublicResultstatus Status {Get;Set; } /// <summary>        ///Operation Exception/// </summary>         PublicException Exception {Get;Set; } /// <summary>        ///Data Results/// </summary>         PublicT Result {Get;Set; } /// <summary>        ///Action Messages/// </summary>         Public stringMessage {Get;Set; } }
View Code

3) Return the front-end data format

   Public classAjaxentity<t>{[Jsonproperty (PropertyName="Success")]         Public BOOLSuccess {Get;Set; } [Jsonproperty (PropertyName="Message")]         Public stringMessage {Get;Set; } [Jsonproperty (PropertyName="Data")]         PublicT Data {Get;Set; } }
View Code

4) Define the Ajax ActionResult in ASP.

  Public classAjaxresult<t>: customresult<ajaxentity<t>>    {        Private ReadOnlyRunresult<t>_runresult;  PublicAjaxresult (runresult<t>Runresult) {_runresult=Runresult; }        /// <summary>        ///         /// </summary>        /// <returns></returns>         Public OverrideAjaxentity<t>GetObject () {ajaxentity<T> entity =Newajaxentity<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; returnentity; }    }
View Code

ASP. NET MVC Ajax processing Ajaxresult

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.