-1

How to return data LINQ in Json
Use this, but no work

    public ActionResult GenerateShop()
    {
        LinqDataContext context = new LinqDataContext();
        IEnumerable<shops> shops = context.shops;            
        return Json(shops.ToList());
    }

This is error "When serializing an object of type "RentApp.Models.bikes" found a circular reference"
Why he writes bikes, when I shop?
It is worked

var json = Json(shops.ToList());

But no work retrun
When I create the model manually everything works fine, but me need LINQ

public List<Shops> ListShop()
    {
        List<Shops> shopList = new List<Shops>();
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = this.connfiguration;
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM shops", conn))
            {
                conn.Open();
                SqlDataReader rdr = cmd.ExecuteReader();                    
                while (rdr.Read())
                {
                    Shops shop = new Shops();
                    shop.id = (int)rdr["id"];
                    shop.name = rdr["name"].ToString();
                    shopList.Add(shop);
                }
                conn.Close();

            }
        }
        return shopList;
    }

In controller

    public ActionResult GenerateShop()
    {
        model.shops = this.rent.ListShop();
        return Json(model.shops);
    }
Vayas
  • 107
  • 3
  • 11
  • 1
    "no work" is a *horrible* description of your problem. Please describe exactly the behavior you get and what you are expecting to happen instead. – lc. Nov 26 '13 at 06:54
  • I do not know, return client server error 500, but if I create a model by hand, everything works fine – Vayas Nov 26 '13 at 06:58
  • Can you show us the definition of the `shops` class please? – rhughes Nov 26 '13 at 07:20
  • **shop** Class VisualStudio generates self, based on a database – Vayas Nov 26 '13 at 07:23
  • The problem is that when you return shops, you most probably have a property called `Bikes` inside it which contains many bikes which in return contains a property called `Shop` which reference back the shop you are trying to return. – Mat J Nov 26 '13 at 07:36

3 Answers3

1

The problem is that when you return shops, you most probably have a property called Bikes inside it which contains many bikes which in return contains a property called Shop which reference back the shop you are trying to return thus causing a circular reference and without further guidelines, will cause the serializer to go on in an infinite loop.

Simplest possible option is to project the required properties from your Shop entity to an anonymous type and pass that to the Json method.

LinqDataContext context = new LinqDataContext();
IEnumerable<shops> shops = context.shops;            
return Json(shops.select(x=>new{x.ShopID,
                                x.ShopProperty2,
                                x.ShopName,
                                ...
                                }));
Mat J
  • 5,422
  • 6
  • 40
  • 56
0

Try this i havnt tried though..

public ActionResult GenerateShop()
    {
        LinqDataContext context = new LinqDataContext();
        var shops = context.shops.ToList();            
        return Json(shops, JsonRequestBehavior.AllowGet);
    }

for more reference see here:-

Why is JsonRequestBehavior needed?

Community
  • 1
  • 1
Neel
  • 11,625
  • 3
  • 43
  • 61
0

i don't know what you want, but you have to add JsonRequestBehaviour.AllowGet to return the JSON data.

Few suggstion use context for one time. create context type in globally then use it.

public JsonResult GetJsonData()
{
 LinqDataContext context = new LinqDataContext();
        var shops = context.shops.ToList();            
        return Json(shops,JsonRequestBehavior.AllowGet);
}
Ravi Gadag
  • 15,735
  • 5
  • 57
  • 83