Query 1: Customer (master, Editing/edit mode all the total 10 columns, but display only 5 cols in grid mode)
Using template editing concept, we can edit the fields which are not present in the grid columns but in dataSource.
Refer to the below code example for setting the external edit template in grid.
@(Html.EJ().Grid<OrderTable>("Grid") .Datasource(ds => ds.URL("GetEmployeeData") .InsertURL("PerformInsert") .UpdateURL("PerformUpdate") .RemoveURL("PerformDelete") .Adaptor(AdaptorType.UrlAdaptor) ) . . . . . . . . .EditSettings(e => e.AllowEditing() .AllowDeleting() .AllowAdding() .EditMode(EditMode.ExternalFormTemplate).ExternalFormTemplateID("#template1") ) . . . . ) <script id="template1" type="text/template"> <b>Employee Details</b> <table cellspacing="10"> . . . //Template comes here </table> </script> |
For external form, we can edit the records using ExternalFormTemplate then we need to set EditMode as ExternalFormTemplate and specify the template ID to ExternalFormTemplateID property.
Refer the below links for online demo more clarification,
http://mvc.syncfusion.com/demos/web/grid/inlineformediting
http://mvc.syncfusion.com/demos/web/grid/externalformediting
Refer to the below online documentation link for more clarification,
http://help.syncfusion.com/aspnetmvc/grid/editing#edit-mode
Query 2: if I use an object with foreign keys as data source, I am getting circular reference. SF grid, what needs to be done to resolve this.
We can avoid circular reference error using “ViewModel” concept. We are highly recommend the ViewModel concept since it will retrieve only required data and avoid the unwanted circular reference error.
Refer to the below link for more details about circular reference error,
http://blogs.microsoft.co.il/gilf/2011/10/17/avoiding-circular-reference-for-entity-in-json-serialization/
http://blog.davebouwman.com/2011/12/08/handling-circular-references-asp-net-mvc-json-serialization/
http://programmers.stackexchange.com/questions/11856/whats-wrong-with-circular-references
For your reference we have attached the sample below with “ViewModel” concept and the same can be downloaded from the following link,
http://www.syncfusion.com/downloads/support/directtrac/general/ze/EntityCodeFirst-1468930071.zip
In the above sample we have generated a ViewModel for the “Orders” table (OrderRepository.cs).
Refer to the below code example,
public static class OrderRepository { public static IList<EditableOrder> GetAllRecords() { . . . . orders = (from ord in new NORTHWNDEntities1().Orders.Take(200) select new EditableOrder { OrderID = ord.OrderID, OrderDate = ord.OrderDate, CustomerID = ord.CustomerID, . . . . }).ToList(); return orders; } |
Regards,
Gowthami V.
@(Html.EJ().Grid<OrderTable>("Grid") .Datasource(ds => ds.URL("GetEmployeeData") .InsertURL("PerformInsert") .UpdateURL("PerformUpdate") .RemoveURL("PerformDelete") .Adaptor(AdaptorType.UrlAdaptor) .EditSettings(e => e.AllowEditing() .AllowDeleting() .AllowAdding() .EditMode(EditMode.InlineFormTemplate).InlineFormTemplateID("#template1") .Columns(col => {
col.Field("EmployeeID").HeaderText("Employee ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add(); col.Field("FirstName").HeaderText("First Name").TextAlign(TextAlign.Right).Width(75).Add(); col.Field("LastName").HeaderText("Last Name").TextAlign(TextAlign.Left).Width(75).Add();
<script id="template1" type="text/template"> <b>Employee Details</b> <table cellspacing="10"> <tr> <td style="text-align: right;"> Employee ID </td> <td style="text-align: left"> <input id="EmployeeID" name="EmployeeID" value="{{: EmployeeID}}" disabled="disabled" class="e-field e-ejinputtext valid e-disable" style="text-align: right; width: 116px; height: 28px" /> </td> <td style="text-align: right;"> FirstName </td> <td style="text-align: left"> <input id="FirstName" name="FirstName" value="{{: FirstName}}" class="e-field e-ejinputtext valid" style="width: 116px; height: 28px" /> </td> </tr> <tr> <td style="text-align: right;"> LastName </td> <td style="text-align: left"> <input type="text" id="LastName" name="LastName" value="{{:LastName}}" /> </td> <td style="text-align: right;"> Title </td> <td style="text-align: left"> <input id="Title" name="Title" value="{{: Title}}" class="e-field e-ejinputtext valid" style="width: 116px; height: 28px" /> </td> </tr> <tr> <td style="text-align: right;"> Ship City </td> <td style="text-align: left"> <input id="City" name="ShipCity" value="{{: City}}" class="e-field e-ejinputtext valid" style="width: 116px; height: 28px" /> </td> </table> </script> |
//Provide grid datasource public ActionResult GetEmployeeData(DataManager dm) {
IEnumerable data = new NORTHWNDEntities1().Employees.ToList(); int count = data.AsQueryable().Count(); DataOperations operation = new DataOperations(); data.Cast<Employee>().Select(x => new { EmployeeID = x.EmployeeID, FirstName = x.FirstName, LastName = x.LastName }); //Performing paging operations data = operation.PerformSkip(data, dm.Skip); data = operation.PerformTake(data, dm.Take); return Json(new { result = data, count = count }, JsonRequestBehavior.AllowGet);
}
//Perform file insertion public ActionResult PerformInsert(Table value) { NORTHWNDEntities1 db = new NORTHWNDEntities1(); db.Tables.Add(value); db.SaveChanges(); return RedirectToAction("GetEmployeeData"); }
//Perform update
public ActionResult PerformUpdate(Table value) {
NORTHWNDEntities1 db = new NORTHWNDEntities1(); Table table = db.Tables.Single(o => o.EmployeeID == value.EmployeeID); db.Entry(table).CurrentValues.SetValues(value); db.Entry(table).State = EntityState.Modified; db.SaveChanges();
return RedirectToAction("GetEmployeeData"); }
//Perform delete public ActionResult PerformDelete(int key) { NORTHWNDEntities1 db = new NORTHWNDEntities1(); db.OrderTables.Remove(db.OrderTables.Single(o => o.OrderID == key)); db.SaveChanges(); return RedirectToAction("GetEmployeeData"); |
public class Student { public Student() {
} public int StudentID { get; set; } public string StudentName { get; set; }
//Foreign key for Standard
[ForeignKey("Standard")] public int StandardRefId { get; set; }
public Standard Standard { get; set; } }
public class Standard { public Standard() {
} public int StandardId { get; set; } public string StandardName { get; set; }
public ICollection<Student> Students { get; set; }
} |
So that only we suggest you to use the “ViewModel” concept to resolve the circular reference issue.
Refer to the below link for prevent circular reference,
http://www.codeproject.com/Articles/38655/Prevent-Circular-References-in-Database-Design
Refer to the below link for more clarification about ViewModel,
http://sampathloku.blogspot.in/2012/10/how-to-use-viewmodel-with-aspnet-mvc.html
http://rachelappel.com/use-viewmodels-to-manage-data-amp-organize-code-in-asp.net-mvc-applications
Regards,
Gowthami V.