locked
MetadataType attribute on partial class not merging class with DataAnnotations on runtime. RRS feed

  • Question

  • User-1593844237 posted
    //------------------------------------------------------------------------------
    // <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; }
            }
        }
    }

    Below is the the html of view 

    @model WebApp.tblEmployee
    
    @{
        ViewBag.Title = "Create";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Create</h2>
    
    @using (Html.BeginForm()) 
    {
        @Html.AntiForgeryToken()
        
        <div class="form-horizontal">
            <h4>Employee</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Department, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Department, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Department, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Gender, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
    
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
    

    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



    Thursday, August 29, 2019 6:53 AM

Answers

  • User-1593844237 posted

    Tried to use the same namespace in both the partial classes and it worked for me.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 29, 2019 8:20 AM