locked
Pass two different list same Json Object RRS feed

  • Question

  • User1655374113 posted

    I am having two different list coming from same object. I want to get those two at same time as seperate list or join those two same list when returning JSON object. Here is my code.

    List<User> userEntity = users.Where(s => s.Id == Id).ToList();
    
            var GetUserNames = userEntity.SelectMany(s => s.Names.Select(u =>
                new
                {
                    Name = u.Name,
                    Id = u.Id
                })).ToList();
    
            var GetProfile = userEntity.SelectMany(s => s.Profile.Select(u =>
              new
              {
                  Name = u.Name,
                  Id = u.Id
              })).ToList();
    
            return Json(GetUserNames, JsonRequestBehavior.AllowGet);

     

    Thursday, February 26, 2015 10:05 AM

Answers

  • User-271186128 posted

    Hi ramll,

    According to your code and description, I suppose you want to return GetUserNames and GetProfile. If that is the case, I suggest you could try to use the following code.

        var data = new { userName= GetUserNames, proFile = GetProfile };
    
        System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
    
        return js.Serialize(data);

    For more details, please refer to this link:

    #SayHelloJson http://forums.asp.net/t/1934215.aspx?Using+jQuery+ajax+to+call+asmx+webservice+methods

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 27, 2015 2:44 AM

All replies

  • User-434868552 posted

    @ramll    TIMTOWTDI

    have you tried?:

    foreach (profile in GetProfile) GetUserNames.Add(profile);
    

    insert the above statement just before your return statement.

    Thursday, February 26, 2015 10:16 AM
  • User-271186128 posted

    Hi ramll,

    According to your code and description, I suppose you want to return GetUserNames and GetProfile. If that is the case, I suggest you could try to use the following code.

        var data = new { userName= GetUserNames, proFile = GetProfile };
    
        System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
    
        return js.Serialize(data);

    For more details, please refer to this link:

    #SayHelloJson http://forums.asp.net/t/1934215.aspx?Using+jQuery+ajax+to+call+asmx+webservice+methods

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 27, 2015 2:44 AM