1

I'm trying to return JSON from the webmethod of IQueryable but instead of JSON I'm getting HTML.
WebMethod must be with UseHttpGet=true signature.
This must create JSON with child nodes: Country node (ID, CountryName) > Region node (RegionID, RegionName) > City node (CityID, CityName) Here is the code:

[System.Web.Services.WebMethod(BufferResponse = false)]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json, UseHttpGet=true)]
public static IQueryable<CountryModel> GenerateJson()
{
    RudenSoftEntities db = new RudenSoftEntities();
    var a = from country in db.Countries
            select new CountryModel
            {
                ID = country.Id,
                CountryName = country.CountryName,
                _region = (from region in db.Regions
                           where region.CountryID == country.Id
                           select new RegionModel
                                   {
                RegionID = region.Id,
                RegionName = region.RegionName,
                 _city = (from city in db.Cities
                          where city.RegionID == region.Id
                          select new CityModel
            {
                  CityID = city.Id,
                  CityName = city.CityName
            }).ToList()
           }).ToList()

       };

       return a;
}

And here is the model:

public class CountryModel
{
    public int ID { get; set; }
    public string CountryName { get; set; }
    public List<RegionModel> _region = new List<RegionModel>();
}

public class RegionModel
{
    public int RegionID { get; set; }
    public string RegionName { get; set; }
    public List<CityModel> _city = new List<CityModel>();
}
public class CityModel
{
    public int CityID { get; set; }
    public string CityName { get; set; }
}
trinaldi
  • 2,872
  • 2
  • 32
  • 37
andrey.shedko
  • 3,128
  • 10
  • 61
  • 121
  • I think that you could actually do this easier using linq along with `JavaScriptSerializer` I will post a simple WebMethod that I just created and tested that returns Json as a String where I am using the linq query against `DataClassesDataContext` – MethodMan Nov 11 '14 at 01:09

1 Answers1

1

This is how I have a simple method that returns JSon

using System.Web.Script.Serialization;
//declared at the class level is my DataClassesDataContext 
DataClassesDataContext dc = new DataClassesDataContext();

[WebMethod (Description = "Get Strapping by passing StapKeyId") ]
public string GetStrapping(string strapKeyId)
{
    var json = string.Empty;
    var railcar = from r in dc.tblRailcars
                  join s in dc.tblStraps on r.TankStrapping_KeyID equals s.KeyId
                  where r.TankStrapping_KeyID == Int32.Parse(strapKeyId)
                  select new 
                  { 
                      r.RailcarMark, 
                      r.RailcarNumber,
                      r.TankStrapping_KeyID,
                      s.Capacity,
                      s.TableNumber,
                      s.TableType
                  };

   JavaScriptSerializer jss = new JavaScriptSerializer();
   json = jss.Serialize(railcar);

    return json;
}

//If you want to see an additional way of doing it by using a Dictionary here is a link to what I have posted last year as well Deserialize a Dynamic JSON Array on C#

Community
  • 1
  • 1
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • not sure but if you maintain the WebService code you should create a test method that retuns a string if you are expecting a List then I can see why you are using the `.ToList()` – MethodMan Nov 11 '14 at 01:23
  • Unfortunately still same problem. – andrey.shedko Nov 11 '14 at 01:29
  • can you step thru the code.. when you get to `a` put a break point also can you return `a.ToList()` I am not sure why you can't use straight Linq query to return JSON.. also are there any `Keys` that share the same value where you should perhaps refactor that linq query to use `JOINS` – MethodMan Nov 11 '14 at 01:31
  • I found solution, problem was on client side ajax call, not server side. – andrey.shedko Nov 11 '14 at 09:31