大家好!我混淆了實現一段代碼,以在幾個案例中使用不同的必需字段(6)的模型在asp.net mvc 3中進行工作.net數據註釋。 我有一個模型:.Net數據註解和模型層次結構
public class OpportunityModel
{
public Guid OpportunityId { get; set; }
[Display(Name = "Value")]
[RegularExpression(@"^[-+]?\d{1,10}(\.\d{0,4})?$", ErrorMessage = "Must be a number")]
public decimal? ActualValue { get; set; }
[Display(Name = "Name")]
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
public string Product { get; set; }
[Display(Name = "Estimated Date")]
public DateTime? EstimateDate { get; set; }
public bool? Sales6ixFallDown { get; set; }
[Display(Name = "Stage")]
public Stages Sales6ixStage { get; set; }
public DateTime? Sales6ixDateInBoard { get; set; }
public DateTime? Sales6ixDateInCurrentStage { get; set; }
public DateTime? Sales6ixNextAppointmentDate { get; set; }
[Display(Name = "Description")]
public string Description { get; set; }
public string Sales6ixNextAppointmentDescription { get; set; }
public int NewColumn { get; set; }
public Guid? CustomerId { get; set; }
public string CustomerName { get; set; }
}
我需要的是可能性動態改變需要封地在裏面。一些谷歌搜索後,這是不可能的,並想出使用模型繼承。我的意思是:我有這樣的基本模型:
public class BaseOpportunityModel
{
public Guid OpportunityId { get; set; }
public virtual decimal? ActualValue { get; set; }
public virtual string Name { get; set; }
public string Product { get; set; }
public DateTime? EstimateDate { get; set; }
public bool? Sales6ixFallDown { get; set; }
[Display(Name = "Stage")]
public Stages Sales6ixStage { get; set; }
public DateTime? Sales6ixDateInBoard { get; set; }
public DateTime? Sales6ixDateInCurrentStage { get; set; }
public DateTime? Sales6ixNextAppointmentDate { get; set; }
[Display(Name = "Description")]
public string Description { get; set; }
public string Sales6ixNextAppointmentDescription { get; set; }
public int NewColumn { get; set; }
public Guid? CustomerId { get; set; }
public string CustomerName { get; set; }
}
其中虛擬屬性是可能是或不是必填字段的屬性。然後,我從基地這樣一個派生的幾個型號:
public class OpportunityModel0: BaseOpportunityModel
{
[Display(Name = "Value")]
[Required(ErrorMessage = "Name is required")]
[RegularExpression(@"^[-+]?\d{1,10}(\.\d{0,4})?$", ErrorMessage = "Must be a number")]
public override decimal? ActualValue { get; set; }
[Display(Name = "Name")]
[Required(ErrorMessage = "Name is required")]
public override string Name { get; set; }
}
,然後我可以在視圖和控制器示範基地BaseOpportunityModel使用。但是我遇到以下問題:
- 驗證使用BaseOpportunityModel中的註釋屬性並忽略派生模型中的屬性。
我該怎麼做?有人可以引導我走向正確的方向,還是可以幫助我解決這個問題?提前致謝。
感謝您的答覆。另一件不起作用的是客戶端驗證。我可以通過在View OpportunityModel0中指定來修復它,但是我需要爲每個派生模型都有這樣的視圖。這不是一個好方法。 – Roman