how to return multiple variables with jsonresult asp.net mvc3

前端 未结 6 1148
半阙折子戏
半阙折子戏 2020-12-24 09:15

How to return multiple variables on JsonResult method

for example i want to return this two variables:

string result = \"Successed\";
string ID = \"         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-24 10:02

    Return an anonymous object.

    return Json( new { Result = result, Id = ID } );
    

    I normally do something like this:

    public enum NoticeTypes
    {
        Default,
        UpdateComplete,
        ResponsePending,
        Notice,
        Error,
        Redirect,
        WaitAndRetryAttempt
    }
    public class AjaxJsonResponse
    {
        public UserNotice Notice { get; set; }
        public object Data { get; set; }
        private AjaxJsonResponse() { }
        public static JsonResult Create(UserNotice Notice,object Data)
        {
            return new JsonResult()
            { 
                Data = new 
                { 
                    Notice = Notice,
                    Data = Data
                } 
            };
        }
    }
    

    So that I can write my javascript to always expect ajax calls to return data in a certain format.

    return AjaxResponse.Create(NoticeTypes.UpdateComplete, new 
    { 
        Result = result, 
        Id = ID 
    });
    

    Now you can do things like an Ajax Complete global handler that can intercept things like Redirect or WaitAndRetry before the normal handler gets it, and to have a standard way of communicating additional information about the returned data that is the same across your application.

提交回复
热议问题