- Home
- Forum
- ASP.NET MVC
- Data-annotation with dynamic datasource
Data-annotation with dynamic datasource
I used data annotations with a typed grid using a ViewModel and they worked both the validation and the text header of the grid. Instead, with a datasource of type IEnumerable <object>, even using a view model does not apply data annotations.
Can data annotations be used with a dynamic datasource?
SIGN IN To post a reply.
5 Replies
SE
Sathyanarayanamoorthy Eswararao
Syncfusion Team
January 2, 2018 06:12 PM UTC
Hi Pio,
Thanks for contacting Syncfusion support.
When DataAnnotations are used the datasource will be fetched based on the class object that is defined for the grid. It does not depend on the model. We have achieved your requirement in the following example.
Refer the code example below.
|
[GridFeatures.cshtml]
@(Html.EJ().Grid<EditableOrder>("FlatGrid")
.Datasource((IEnumerable<EditableOrder>)ViewBag.dataSource)
.AllowPaging()
.EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })
.ToolbarSettings(toolbar =>
{
toolbar.ShowToolbar().ToolbarItems(items =>
{
items.AddTool(ToolBarItems.Add);
items.AddTool(ToolBarItems.Edit);
items.AddTool(ToolBarItems.Delete);
items.AddTool(ToolBarItems.Update);
items.AddTool(ToolBarItems.Cancel);
});
})
)
[Editableorder.cs]
public class EditableOrder
{
[Display(Name = "Order ID")]
[Required(ErrorMessage = "OrderID is must")]
public int OrderID
{
get;
set;
}
[Display(Name = "Emp ID")]
[Required(ErrorMessage = "EmployeeID is must")]
public int? EmployeeID
{
get;
set;
}
[Display(Name = "City")]
public string ShipCity
{
get;
set;
}
[Display(Name = "Country")]
public string ShipCountry
{
get;
set;
}
[Display(Name = "Freight")]
public decimal? Freight
{
get;
set;
}
public bool Verified
{
get;
set;
}
|
If you need any further assistance please get back to us.
Regards,
Sathyanarayanamoorthy
PL
Pio Luca Valvona
January 2, 2018 11:21 PM UTC
Thank you Sathyanarayanamoorthy,
but I'm having the same problem even with a childgrid. In this case the data is retrieved by JSON.
VIEW:
.ChildGrid(cg=>cg.Datasource(ds => ds.URL(@Url.Action("Clienti_Servizi","Admin")).Adaptor(AdaptorType.UrlAdaptor)).QueryString("IDUtente").Locale(CultureInfo.CurrentCulture.TextInfo.CultureName).AllowPaging().IsResponsive(true) .Columns(col=> { col.Field("IDServizio").IsPrimaryKey(true).Visible(false).Add(); col.Field("NomeServizio").Add(); }))
CONTROLLER
public async Task<JsonResult> Clienti_Servizi(DataManager dm) { log4net.Config.XmlConfigurator.Configure(); log.Info("Inizio esecuzione Clienti_Servizi(Guid idUtente)"); List<ServiziVM> data = new List<ServiziVM>(); int count = 0; try { IEnumerable<ServizioCliente> servizi; servizi = await _servizioClienteRepository.GetServiziCliente(Guid.Parse(dm.Where[0].value.ToString())); if (servizi.Any()) { foreach (ServizioCliente sc in servizi) { Servizio s = new Servizio(); s = await _servizioRepository.GetServizio(false, sc.IDServizio); ServiziVM svm = new ServiziVM() { Deleted = s.Deleted, DescrizioneServizio = s.DescrizioneServizio, IDServizio = s.IDServizio, NomeServizio = s.NomeServizio }; data.Add(svm); } } count = data.Count(); JsonResult test = new JsonResult(); test = Json(new { result = data, count }); log.Info("Fine esecuzione Clienti_Servizi(Guid idUtente)"); } catch (Exception ex) { log.Error(string.Format("Errore:{0}", ex.InnerException == null ? ex.Message : ex.InnerException.Message)); throw ex; } return Json(new { result = data, count }); }
CLASS
public class ServiziVM { [Key] public Guid IDServizio { get; set; } [Display(Name = "NomeServizio", ResourceType = typeof(Resources.Servizi))] [Required(ErrorMessageResourceType = typeof(Resources.Common), ErrorMessageResourceName = "CampoObbligatorio")] public string NomeServizio { get; set; } [Display(Name = "DescrizioneServizio", ResourceType = typeof(Resources.Servizi))] [Required(ErrorMessageResourceType = typeof(Resources.Common), ErrorMessageResourceName = "CampoObbligatorio")] public string DescrizioneServizio { get; set; } [Display(Name = "Eliminato", ResourceType = typeof(Resources.Servizi))] public bool Deleted { get; set; } }
I take the opportunity to ask you if I can do CRUD operations on child tables.
Luca
SE
Sathyanarayanamoorthy Eswararao
Syncfusion Team
January 3, 2018 05:47 PM UTC
Hi Pio,
As per your requirement we have created a grid with dataAnnotations and the childgrid with JSON datasource using urlAdaptor. You can perform the CRUD operations such as Delete or Add or Edit the data in the childGrid. Please refer the code example below.
|
@(Html.EJ().Grid<EditableOrder>("FlatGrid")
.Datasource(Model)
.AllowPaging()
.EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })
.ToolbarSettings(toolbar =>
{
toolbar.ShowToolbar().ToolbarItems(items =>
{
items.AddTool(ToolBarItems.Add);
items.AddTool(ToolBarItems.Edit);
items.AddTool(ToolBarItems.Delete);
items.AddTool(ToolBarItems.Update);
items.AddTool(ToolBarItems.Cancel);
});
})
.Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Add(); ;
col.Field("CustomerID").HeaderText("Customer ID").Add(); ;
col.Field("EmployeeID").HeaderText("Employee ID").Add();
col.Field("ShipCity").HeaderText("Ship City").Add();
})
.ChildGrid(child =>
{
child.Datasource(ds => ds.URL("/Grid/DataSource").UpdateURL("NormalUpdate").InsertURL("NormalInsert").RemoveURL("NormalDelete").Adaptor(AdaptorType.UrlAdaptor))
.QueryString("EmployeeID")
.AllowPaging()
.EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })
.ToolbarSettings(toolbar =>
{
toolbar.ShowToolbar().ToolbarItems(items =>
{
items.AddTool(ToolBarItems.Add);
items.AddTool(ToolBarItems.Edit);
items.AddTool(ToolBarItems.Delete);
items.AddTool(ToolBarItems.Update);
items.AddTool(ToolBarItems.Cancel);
});
})
.Columns(col =>
{
col.Field("OrderID").HeaderText("OrderID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(75).Add();
col.Field("ShipCity").HeaderText("ShipCity").Width(100).Add();
col.Field("Freight").Width(120).Add();
col.Field("ShipName").Width(100).Add();
});
}))
[controller]
public List<Orders> BindDataSource()
{
int code = 10000;
for (int i = 1; i < 50; i++)
{
order.Add(new Orders(code + 1, "TOMSP", i + 0, 2.3 * i, "M�nster", "Toms Spezialit�ten", new DateTime(1991, 05, 15), "Germany", "44087", false));
order.Add(new Orders(code + 2, "HANAR", i + 2, 3.3 * i, "Rio de Janeiro", "Hanari Carnes", new DateTime(1990, 04, 04), "Brazil", "05454-876", true));
order.Add(new Orders(code + 3, "VICTE", i + 1, 4.3 * i, "Lyon", "Victuailles en stock", new DateTime(1957, 11, 30), "France", "69004", true));
order.Add(new Orders(code + 4, "VINET", i + 3, 5.3 * i, "Reims", "Vins et alcools Chevalier", new DateTime(1930, 10, 22), "France", "51100", true));
order.Add(new Orders(code + 5, "SUPRD", i + 4, 6.3 * i, "Charleroi", "Supr�mes d�lices", new DateTime(1953, 02, 18), "Belgium", "B-6000", false));
code += 5;
}
return order;
}
public ActionResult NormalUpdate([FromBody]CRUDModel<Orders> myObject)
{
var ord = myObject.Value;
Orders val = order.Where(or => or.OrderID == ord.OrderID).FirstOrDefault();
val.OrderID = ord.OrderID;
val.EmployeeID = ord.EmployeeID;
val.CustomerID = ord.CustomerID;
val.Freight = ord.Freight;
val.ShipCity = ord.ShipCity;
return Json(myObject.Value);
}
public ActionResult NormalInsert([FromBody]CRUDModel<Orders> value)
{
order.Insert(order.Count, value.Value);
return Json(order);
}
public ActionResult NormalDelete([FromBody]CRUDModel<Orders> value)
{
order.Remove(order.Where(or => or.OrderID == int.Parse(value.Key.ToString())).FirstOrDefault());
return Json(value);
}
[Serializable]
public class Orders
{
public Orders()
{
}
public Orders(long OrderId, string CustomerId, int EmployeeId, double Freight, string ShipCity, string ShipName,
DateTime OrderDate, string ShipCountry, string ShipPostalCode, bool Verified)
{
this.OrderID = OrderId;
this.CustomerID = CustomerId;
this.EmployeeID = EmployeeId;
this.Freight = Freight;
this.ShipCity = ShipCity;
this.ShipName = ShipName;
this.OrderDate = OrderDate;
this.ShipCountry = ShipCountry;
this.ShipPostalCode = ShipPostalCode;
this.Verified = Verified;
}
public long OrderID { get; set; }
public string CustomerID { get; set; }
public int EmployeeID { get; set; }
public double Freight { get; set; }
public string ShipCity { get; set; }
public string ShipName { get; set; }
public string ShipCountry { get; set; }
public string ShipPostalCode { get; set; }
public bool Verified { get; set; }
public DateTime OrderDate { get; set; }
}
public ActionResult DataSource(DataManager dm)
{
IEnumerable Data = BindDataSource();
int count = Data.AsQueryable().Count();
Syncfusion.JavaScript.DataSources.DataOperations operation = new Syncfusion.JavaScript.DataSources.DataOperations();
Data = operation.Execute(Data, dm);
return Json(new { result = Data, count = count }, JsonRequestBehavior.AllowGet);
}
|
We have prepared the sample which can be downloaded from the below location.
Regards,
Sathyanarayanamoorthy
EJ
Emma Jasmine
January 8, 2021 10:50 AM UTC
Thanks
PS
Pon Selva Jeganathan
Syncfusion Team
January 11, 2021 09:42 AM UTC
Hi Emma Jasmine ,
You are most welcome.
Please get back to us if you need any further assistance.
Regards,
Pon selva
SIGN IN To post a reply.
- 5 Replies
- 4 Participants
-
PL Pio Luca Valvona
- Dec 30, 2017 11:15 PM UTC
- Jan 11, 2021 09:42 AM UTC