如何获取列表<>结果是json格式 [英] How to Get List<> result in json format

查看:126
本文介绍了如何获取列表<>结果是json格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获得一个List< users>以Json格式

我试过以下

我的KnockoutJS为



  var  self =  this ; 
self.http = new httpWrapper();
self.GetAllUserForApproval = function (){
self.http.post('' / Lending / Account / GetAllUsersForApproval'
.done( function (data){
alert(data.Payload);
});
};





我的控制器



 [HttpPost] 
public JsonResult GetAllUsersForApproval()
{
return Json(AccountRepo.GetUserForApproval());
}





AccountRepo.GetUserForApproval()定义类似于



  public 列表<用户> GetUserForApproval()
{
return Context.Users
.Where(x = > ; x.IsConfirmationPending).ToList< User>()?? new 列表< User>();
}







问题在于执行self.http.post('/ Lending / Account / GetAllUsersForApproval')

它显示错误



无法加载资源:服务器响应状态为500(内部服务器错误),



但如果我将AccountRepo.GetUserForApproval()定义更改为



< pre lang =c#> public 用户GetUserForApproval()
{
return Context.Users
.Where(x = > x.IsConfirmationPending).FirstOrDefault()?? new User();
}



它工作正常我得到了js成功函数的结果,

但是我想要用户列表

Plz帮助我..

解决方案

我可以建议你在这里使用一点防守编码......



您的GetUserForApproval()函数应该有一些错误处理... 500错误表明您的函数或JSON编码器可能会发生某种未处理的异常......



您应该在代码周围添加一个try ... catch块,以查看编码数据是否有错误......


I needs to get a List<users> in Json format
I have tried the following
My KnockoutJS as

var self = this;
self.http = new httpWrapper();
self.GetAllUserForApproval = function () {
    self.http.post('/Lending/Account/GetAllUsersForApproval')
            .done(function (data) {
                alert(data.Payload);
            });
};



My Controller As

[HttpPost]
public JsonResult GetAllUsersForApproval()
{
    return Json(AccountRepo.GetUserForApproval());
}



The AccountRepo.GetUserForApproval() definition is like

public List<User> GetUserForApproval()
{
    return Context.Users
        .Where(x => x.IsConfirmationPending).ToList<User>() ?? new List<User>();
}




The Problem is when executing the self.http.post('/Lending/Account/GetAllUsersForApproval')
it shows the error as

Failed to load resource: the server responded with a status of 500 (Internal Server Error),

But if i change the AccountRepo.GetUserForApproval() definition to

public User GetUserForApproval()
        {
            return Context.Users
                .Where(x => x.IsConfirmationPending).FirstOrDefault() ?? new User();
        }


it works fine and i got the result in js success function ,
But i want List of users
Plz help me..

解决方案

Could I suggest that you use a little defensive coding here...

Your GetUserForApproval() function should have some error handling... The 500 error suggest that some sort of unhandled exception could be happening either in your function or by the JSON encoder...

You should add a try ... catch block around your code to see if there is an error encoding your data...


这篇关于如何获取列表&lt;&gt;结果是json格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆