Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I needs to get a List<users> in Json format
I have tried the following
My KnockoutJS as

JavaScript
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

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


The AccountRepo.GetUserForApproval() definition is like

C#
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

C#
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..
Posted

1 solution

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...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900