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);
}