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

Entity framework database first Data-Annotations

Hello,

Iam using Database First with [Required] Annotations in Metadata.cs for my Models. As it is not a good idea to place it to the generated cs Files of the Entity Framework. With the next Update of the Database the Annotations would be deleted, so for that case we use the MetadataTypes:

THis is the Original EF File:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Website_Backend.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class Kunden
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Kunden()
        {
            this.Touren = new HashSet<Touren>();
            this.Preismodell = new HashSet<Preismodell>();
            this.Touren = new HashSet<Touren>();
            this.KundenRechnung = new HashSet<KundenRechnung>();
        }
    
        public int Id { get; set; }
        public string Name { get; set; }
        public string Strasse { get; set; }
        public string Plz { get; set; }
        public string Ort { get; set; }
        public string Land { get; set; }
........

THis is our Partial Classes.cs:
using System;
using System.ComponentModel.DataAnnotations;

namespace Website_Backend.Models
{
    [MetadataType(typeof(MitarbeiterMetadata))]
    public partial class Mitarbeiter
    {
    }

    [MetadataType(typeof(MitarbeiterTypMetadata))]
    public partial class MitarbeiterTyp
    {
    }
}

This is The Metadata.cs:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Script.Serialization;

namespace Website_Backend.Models
{
    public class MitarbeiterMetadata
    {
        [Required]
        public string Personalnummer;

        [Required]
        public string Vorname { get; set; }

        [Required]
        public string Nachname { get; set; }

-------------------------------------------------------------------

So is it possible to get this Annotations working in Syncfusion Grid????

Thanks

6 Replies

JK Jayaprakash Kamaraj Syncfusion Team June 6, 2016 12:50 PM UTC

Hi Arthur, 

Thank you for contacting Syncfusion support. 

Yes, it is working in Syncfusion Grid. We have created a sample based on your requirement. In that sample we have handled validations in server side grid operations(insert/update/delete) and also we have displayed error message in alert box using ActionFailure event . ActionFailure event triggered for every grid action server failure event. Please refer to the below code example and sample. 


@(Html.EJ().Grid<object>("FlatGrid") 
           .Datasource(ds => ds.URL("/Grid/DataSource").UpdateURL("/Grid/Update").InsertURL("/Grid/Insert").Adaptor(AdaptorType.UrlAdaptor)) 
     
        }).ClientSideEvents(eve => { eve.ActionFailure("Failure"); })) 
<script type="text/javascript"> 
    function Failure(args) { 
        var popupObj = $("#FlatGrid").data("ejWaitingPopup"); 
        popupObj.hide(); 
        this.cancelEdit(); 
        var str = ""; 
        str = args.error.statusText; 
        alert(str); 
    } 
</script> 
 
GridController.cs 
public ActionResult DataSource(DataManager dm) 
        { 
            DataOperations ds = new DataOperations(); 
 
            var data = ds.Execute(OrderRepository.GetAllRecords().ToList(), dm); 
            return Json(new { result = data, count = db.Orders.ToList().Count }, JsonRequestBehavior.AllowGet); 
        } 
 
        public ActionResult Update(EditableOrder value) 
        { 
            if (!ModelState.IsValid) 
            { 
                var message = string.Join(" | ", ModelState.Values 
       .SelectMany(v => v.Errors) 
       .Select(e => e.ErrorMessage)); 
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, message); 
            } 
            EditableOrder old = OrderRepository.GetAllRecords().Where(o => o.OrderID == value.OrderID).SingleOrDefault(); 
            return Json(OrderRepository.GetAllRecords(), JsonRequestBehavior.AllowGet); 
        } 


If we have misunderstood your requirement, please share more details about requirement to validate the issue at our end.  

Regards, 

Jayaprakash K. 



AR Arthur June 6, 2016 12:58 PM UTC

Thanks a lot for this Example.

Is it possible to automaticly generate the client Side Validations, if i set [Required]-Annotation to my Entity Model?


JK Jayaprakash Kamaraj Syncfusion Team June 7, 2016 01:10 PM UTC

Hi Arthur, 

Yes, it is possible to automatically generate the client Side Validations, if you set [Required]-Annotation to your Entity Model. 

We have prepared a sample with the Data Annotation and it can be downloaded from the following location. 


We have prepared Order class which holds the data annotation properties of the required columns of Grid and its class name can be given as type of object for the Grid as shown below. Refer to the following code example and screenshot. 

GridFeatures.cshtml 
@(Html.EJ().Grid<SyncfusionMvcApplication90.Models.Order>("FlatGrid") 
               .Datasource((IEnumerable<object>)ViewBag.datasource) 
        })) 
public partial class Order 
    { 
        
        public int OrderID { get; set; } 
         [Required(ErrorMessage = "CustomerID is required")]  
        public string CustomerID { get; set; } 
        public Nullable<int> EmployeeID { get; set; } 
        public Nullable<System.DateTime> OrderDate { get; set; } 
        public Nullable<System.DateTime> RequiredDate { get; set; } 
        public Nullable<System.DateTime> ShippedDate { get; set; } 
        public Nullable<int> ShipVia { get; set; } 
        public Nullable<decimal> Freight { get; set; } 
        public string ShipName { get; set; } 
        public string ShipAddress { get; set; } 
        public string ShipCity { get; set; } 
        public string ShipRegion { get; set; } 
        public string ShipPostalCode { get; set; } 
        public string ShipCountry { get; set; } 
     
        public virtual Employee Employee { get; set; } 
    } 

 

Regards, 
Jayaprakash K. 



AR Arthur June 8, 2016 08:45 AM UTC

How can i display the Message like in your Screenshot as an Tooltip on bottom of the Column?
 I've just got working your Example with the JS-Function and the Altert: }).ClientSideEvents(eve => { eve.ActionFailure("Failure"); })) 



AR Arthur June 9, 2016 07:30 AM UTC

Any Suggestins? I'm working with "CrudUpdate"


JK Jayaprakash Kamaraj Syncfusion Team June 9, 2016 01:39 PM UTC

Hi Arthur, 
We have prepared Order class which holds the data annotation properties of the required columns of Grid and its class name can be given as type of object for the Grid as shown below. We need to refer  jquery.validate.min.js and jquery.validate.unobtrusive.min.js scripts for validation. So, please refer to these script files in your sample. Also refer to the below code example and help documentation link. 
    <script src="Scripts/jquery.validate.min.js"></script> 
    <script src="Scripts/jquery.validate.unobtrusive.min.js"></script> 

GridFeatures.cshtml 
@(Html.EJ().Grid<SyncfusionMvcApplication90.Models.Order>("FlatGrid") 
               .Datasource((IEnumerable<object>)ViewBag.datasource) 
        })) 
public partial class Order 
    { 
        
        public int OrderID { get; set; } 
         [Required(ErrorMessage = "CustomerID is required")]  
        public string CustomerID { get; set; } 
        public Nullable<int> EmployeeID { get; set; } 
        public Nullable<System.DateTime> OrderDate { get; set; } 
        public Nullable<System.DateTime> RequiredDate { get; set; } 
        public Nullable<System.DateTime> ShippedDate { get; set; } 
        public Nullable<int> ShipVia { get; set; } 
        public Nullable<decimal> Freight { get; set; } 
        public string ShipName { get; set; } 
        public string ShipAddress { get; set; } 
        public string ShipCity { get; set; } 
        public string ShipRegion { get; set; } 
        public string ShipPostalCode { get; set; } 
        public string ShipCountry { get; set; } 
     
        public virtual Employee Employee { get; set; } 
    } 


Regards, 
Jayaprakash K. 


Loader.
Live Chat Icon For mobile
Up arrow icon