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