I have build a very very simple database schema as
**Tickets**:
TicketId (PK)
AreaId (FK)
SeverityId (FK)
AssigneeId (FK)
**Areas**:
AreaId(PK)
AreaName
**Severities**:
SeverityId(PK)
SeverityName
**Assignees**:
AssigneeId(PK)
AssigneeName
I create the Entity Model and in the TicketController I am trying to retrieve and Json the data inside the Tickets table.
[HttpPost]
public ActionResult Index([DataSourceRequest]DataSourceRequest result)
{
var tickets = db.Tickets.Include(t=> t.Areas).Include(t=> t.Assignees).Include(t=> t.Severities)
DataSourceResult result = tickets.ToDataSourceResult(request);
Return Json(result);
}
For a reason that I cannot understand when I Json the result in order to pass it to a KendoUiGrid in the view, I get the “circular reference error”. I see no circular reference in my tables/entities relations!!
In the Kendo documentation I read this
If you decide to use the Kendo UI JavaScript libraries directly,
returning JSON is simple. You just need to defined a controller
method and call this.Json on your return result. Provided .NET can
serialize your response, this is all there is to it.
Does all the above mean that in reality I cannot Json entities that come from sane database schemas with primary/foreign keys?
Thanx in advance
Create a flat Model class and pass the model to Json and Grid. You will have to populate the model a little more manually than what you are trying to do here, but there are other benefits to having a model that you will discover later. The circular reference is because of the navigational properties of your EF classes. In MVC the model does not mean the data model classes, but new ones you create. Done right the new model should do most of the work, validation, translation, etc. The controllers just passes things around, the view just renders. Hope this helps!