Yasir Mughal
Yasir Mughal

Reputation: 83

Data Annotations for MVC4

The problem is, i want to implement data-annotation in POCCO Enitites But after "edmx update" all me validation is removed, for doing it, i do R&D for below example but not getting successful. I shared with all of you my senerio, please someone answer it.

POCCO Entity

namespace FGSMVC
{
    using System;
    using System.Collections.Generic;

    public partial class DeclaredBill
    {
        public DeclaredBill()
        {
            this.DeclaredBillReciepts = new HashSet<DeclaredBillReciept>();
        }

        public string DocNo { get; set; }
        public string BCCode { get; set; }
        public string BillNo { get; set; }
        public Nullable<System.DateTime> BillDate { get; set; }
        public string PONumber { get; set; }
        public string CustomerCode { get; set; }
        public Nullable<double> Amount { get; set; }
        public bool IsPaid { get; set; }
        public bool IsActual { get; set; }

        public virtual BusinessConcern BusinessConcern { get; set; }
        public virtual ICollection<DeclaredBillReciept> DeclaredBillReciepts { get; set; }
        public virtual PartyDetail PartyDetail { get; set; }
    }
}

MY Model Class

namespace FGSMVC.Models
{
    [MetadataType(typeof(DeclaredBillModel))]
    public partial class DeclaredBill
    {
    }

    public class DeclaredBillModel
    {

        [Display(Name = "Doc No")]
        public string DocNo;

        [Display(Name = "Business Concern")]
        public string BCCode;

        [Display(Name = "Bill #")]
        public string BillNo;

        [Display(Name = "Bill Date")]
        public Nullable<System.DateTime> BillDate;

        [Display(Name = "PO #")]
        public string PONumber;

        [Display(Name = "Customer")]
        public string CustomerCode;

        [Display(Name = "Amount")]
        public Nullable<double> Amount;

        [Display(Name = "Paid")]
        public bool IsPaid;

        [Display(Name = "Acutal")]
        public bool IsActual;
    }
}

As result is given below


Please some one solve my problem, i am in trouble, In my point of view label must be shown as Display name data-annotation

Upvotes: 1

Views: 1320

Answers (1)

Dmytro
Dmytro

Reputation: 1600

Try using automatic properties instead of ordinal fields in your metadata class (DeclaredBillModel):

[Display(Name = "Doc No")]
public string DocNo {get;set;}

And here is another problem... Partial class (DeclaredBill) cannot be defined in 2 different namespaces. With your code you defined 2 different DeclaredBill classes, and only one of them (not the one you're using in your View), has annotation. So move your code:

[MetadataType(typeof(DeclaredBillModel))]
public partial class DeclaredBill
{
}

to namespace FGSMVC, from FGSMVC.Models.

Upvotes: 1

Related Questions