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. Image for the cookie policy date

Retrieve Data Annotations

Is there a way to use the data annotations from my models to easy the conversion from razor views and reduce the potential errors during entry.  

According to thread 108690 it can be done in ASP.NET MVC(Classic) .  But I can't download the example to check if I am doing something wrong in ASP.NET Core.

Regards
Jim

4 Replies

MG Michael Gerfen August 28, 2017 05:44 PM UTC

I'm facing the same situation.  The aspnet-core documentation is an missing the data-annotation page from aspnetmvc: https://help.syncfusion.com/aspnetmvc/grid/data-annotation.

 



SA Saravanan Arunachalam Syncfusion Team August 29, 2017 06:31 AM UTC

Hi Michael, 
Thanks for contacting Syncfusion’s support. 
We have analyzed your requirement Data annotation on Asp.Net core application with Syncfusion Grid and achieved it by using Html helper on core application that can be refer from the below code example. 
[View] 
@using Syncfusion.JavaScript.Models 
@model IEnumerable<Orders> 
 
@{ 
    Html.EJ().Grid<Orders>("FlatGrid").Datasource(Model).AllowPaging().Render(); 
} 
[Model] 
public class Orders 
    { 
        . . . 
 
        [Display(Name = "Order ID")] 
        public long OrderID { get; set; } 
 
        public string CustomerID { get; set; } 
        [Display(Name = "Emp ID")] 
        public int EmployeeID { get; set; } 
        [Display(Name = "Freight")] 
        public double Freight { get; set; } 
        public DateTime OrderDate { get; set; } 
        [Display(Name = "City")] 
        public string ShipCity { get; set; } 
        [Display(Name = "Country")] 
        public string ShipCountry { get; set; } 
    } 
[Controller] 
public IActionResult Index() 
        { 
            List<Orders> data = GetData(); 
            return View(data); 
        } 
 
And also we have created a sample that can be downloaded from the below link, 
Regards, 
Saravanan A. 



JW James Wilkinson August 29, 2017 03:39 PM UTC

Thanks for the reply. I am using ViewModels that inherit from Models.  It looks like the annotations defined in the Model are available in the View but not the ones from the ViewModel.

I've attached a project that illustrates this.  I created a new project using the latest asp.net core 2.0 templates in VS2017 since I could not actually run your example.  My system attempted to upgrade it and it does not run.  

In the test project, the property "Enabled" is defined in the ViewModel and its annotation is ignored.  The other properties are defined in the Model and are displayed.

Regards

Jim


Attachment: WebApplicationTest_68cc8305.7z


SA Saravanan Arunachalam Syncfusion Team August 31, 2017 12:54 PM UTC

Hi James, 
We have analyzed your provided sample, the cause of the issue is that you have rendered the Grid object based on the view model as base class (Order) instead of using derived class (OrdersViewModel).  
So, we suggest you to render the Grid based on the derived class object which includes both base and derived class annotation properties on the Grid control that can be refer from the below code example. 
{ 
    Html.EJ().Grid<OrderViewModel>("FlatGrid") 
       . . .         
} 
 
Regards, 
Saravanan A. 


Loader.
Up arrow icon