//------------------------------------------------------------------------------
// <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 WebApp
{
using System;
using System.Collections.Generic;
public partial class tblEmployee
{
public int EmployeeID { get; set; }
public string Name { get; set; }
public string City { get; set; }
public string Department { get; set; }
public string Gender { get; set; }
}
}
Above model is generated using edmx and I am trying to add DataAnnotations. So, I am using MetadataType attribute like below code snippet and It's doesn't work as expected
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace WebApp.Models
{
[MetadataType(typeof(EmployeeMetaData))]
public partial class tblEmployee
{
class EmployeeMetaData
{
[Required(ErrorMessage = "Full Name is required")]
[Display(Name = "Full Name")]
public string Name { get; set; }
[Required(ErrorMessage = "City Name is required")]
[Display(Name = "City Name")]
public string City { get; set; }
[Required(ErrorMessage = "Department Name is required")]
[Display(Name = "Department Name")]
public string Department { get; set; }
[Required(ErrorMessage = "Gender is required")]
public string Gender { get; set; }
}
}
}
On submit the blank form then it's doesn't show any validation error and post the data to controller with Model.IsValid =true. I am proving the link of blank form and controller action method with valid model using below links. on submit Blank form
and Valid model after blank form submit
Member
3 Points
55 Posts
MetadataType attribute on partial class not merging class with DataAnnotations on runtime.
Aug 29, 2019 06:53 AM|itsathere|LINK
Above model is generated using edmx and I am trying to add DataAnnotations. So, I am using MetadataType attribute like below code snippet and It's doesn't work as expected
Below is the the html of view
On submit the blank form then it's doesn't show any validation error and post the data to controller with Model.IsValid =true. I am proving the link of blank form and controller action method with valid model using below links.
on submit Blank form and Valid model after blank form submit
Member
3 Points
55 Posts
Re: MetadataType attribute on partial class not merging class with DataAnnotations on runtime.
Aug 29, 2019 08:20 AM|itsathere|LINK
Tried to use the same namespace in both the partial classes and it worked for me.