Mona Coder Mona Coder - 2 months ago 6
ASP.NET (C#) Question

Not Able to Access Data Annotation From Partial Class

Using ASP.NET MVC pattern I have an Entity Framework stored in the

Models->Northwind
which includes
Region.cs
class. I also have a
Partial
Folder which contains
RegionalPartial.cs
which includes Data annotation for
region.cs


Here is the Directory structure

enter image description here

The
Region.cs
looks like :

namespace Map.Models.Northwind
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

public partial class Region
{
public int RegionID { get; set; }

public string RegionDescription { get; set; }
}
}


and the
RegionPartial.cs
is like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Map.Models.Northwind.Partials
{
using System.ComponentModel.DataAnnotations;
[MetadataType(typeof(RegionMetaData))]
public partial class Region {}

public class RegionMetaData
{
[Required]
[Display(Name = "REGION DESCRIPTION")]
public object RegionDescription { get; set; }

}
}


but the
Region.cs
is not using the data annotation stored in the
RegionPartial.cs
! can you please let me know why this is happening and how I can fix this?

Answer

Change the namespace in RegionPartial.cs from

Map.Models.Northwind.Partials

to

Map.Models.Northwind
Comments