We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Effects of Changing Navigation Property to Prevent Circular Reference During JSON Serialization

Thread ID:

Created:

Updated:

Platform:

Replies:

126077 Sep 22,2016 02:20 AM UTC Sep 22,2016 01:54 PM UTC ASP.NET MVC 1
loading
Tags: Grid
James Bergen
Asked On September 22, 2016 02:20 AM UTC

I am trying to implement a editable grid in my existing ASP.NET MVC5 Entity Framework 6 database first project.

My db has two tables as follows:
tblUsers
--------
User_ID    (PK)
User_Name
Company_ID   (FK)

tblCompany
----------
Company_ID    (PK)
Company_Name

I am trying to get a grid that will show User_ID, User_Name, Company_ID and Company_Name.
Due to the way JSON interacts with Entity Framework when serializing the query there is a circular reference as discussed in the Knowledge Base at this link:
https://www.syncfusion.com/kb/6290/circular-reference-error-while-using-entity-framework-linq-2-sql

If possible I do not want to have to make ViewModels so I have tried to just change the navigation property on tblUsers to Internal. My grid displays data for all the fields requested except Company_Name. Can I get it to show with the navigation property set to internal?

Thank you,
James

Code below:

View:
-------
@model IEnumerable<SyncFusion_GridTest.Models.tblUser>

@(Html.EJ().Grid<object>("Grid")
           .Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.datasource).UpdateURL("/tblUsers/Edit")
                               .InsertURL("/tblUsers/Create").RemoveURL("/tblUsers/Delete")
           .Adaptor(AdaptorType.RemoteSaveAdaptor)
              )
        .Query("new ej.Query().from('tblUser')")
        .AllowPaging()
        .AllowSorting()
        .EditSettings(e => e.AllowEditing()
                            .AllowDeleting()
                            .AllowAdding()
                            .EditMode(EditMode.Normal)
                                )
        .ToolbarSettings(tool => tool.ShowToolbar().ToolbarItems(item =>
        {
            item.AddTool(ToolBarItems.Add);
            item.AddTool(ToolBarItems.Edit);
            item.AddTool(ToolBarItems.Delete);
            item.AddTool(ToolBarItems.Update);
            item.AddTool(ToolBarItems.Cancel);
        }))

        .Columns(col =>
        {

            col.Field("User_ID").HeaderText("User_ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
            col.Field("User_Name").HeaderText("User_Name").TextAlign(TextAlign.Right).Width(75).Add();
            col.Field("Company_ID").HeaderText("Company_ID").TextAlign(TextAlign.Left).Width(75).Add();
            col.Field("Company_Name").HeaderText("Company_Name").TextAlign(TextAlign.Left).Width(75).Add();
        }))

---------
Controller:

namespace SyncFusion_GridTest.Controllers
{
    public class tblUsersController : Controller
    {
        private SyncFusionTestEntities db = new SyncFusionTestEntities();
       
        // GET: NewOrders
        public ActionResult Index()
        {
            var data = new SyncFusionTestEntities().tblUsers.Include(t => t.tblCompany);
            ViewBag.datasource = data.ToList();
            return View(data);
        }
.
.
.

---------
Models:
 public partial class tblUser
    {
        public long User_ID { get; set; }
        public string User_Name { get; set; }
        public long Company_ID { get; set; }
   
        public virtual tblCompany tblCompany { internal get; set; }
    }


public partial class tblCompany
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public tblCompany()
        {
            this.tblUsers = new HashSet<tblUser>();
        }
   
        public long Company_ID { get; set; }
        public string Company_Name { get; set; }
   
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<tblUser> tblUsers { internal get; set; }
    }

Jayaprakash Kamaraj [Syncfusion]
Replied On September 22, 2016 01:54 PM UTC

Hi James, 

Thank you for contacting Syncfusion support. 

To overcome this problem we suggest you to use foreign key column Feature . DataSource can be bound to datasource property of columns. Data field and text can be set using foreignKeyField and foreignKeyValue properties of columns. Please refer to the below help document, code example and sample. 
 
 
@(Html.EJ().Grid<object>("FlatGrid") 
                .Datasource((IEnumerable<object>)ViewBag.dataSource1) 
            … 
 
       .Columns(col => 
        { 
            col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(85).ValidationRules(v => v.AddRule("required", true).AddRule("number", true)).Add(); 
            col.Field("CustomerID").HeaderText("Customer ID").Width(90).EditType(EditingType.String).ValidationRules(v => v.AddRule("required", true).AddRule("minlength", 3)).Add(); 
            col.Field("EmployeeID").HeaderText("Employee Name").ForeignKeyField("EmployeeID").ForeignKeyValue("FirstName").DataSource((IEnumerable<object>)ViewBag.dataSource2).TextAlign(TextAlign.Left).Width(90).Add();   
             
        })) 



Jayaprakash K. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;