2012-07-18 103 views
0

採用這種模式:EF4 MVC3模型狀態驗證

public class Cases 
    { 
    //case data model for call center 
    //implement lists for all related child tables too 

    [Key] 
    public int CasesID { get; set; } 

    public string CaseNumber { get; set; } 

    [Required(ErrorMessage = "Customer is Required")] 
    public int CustomerID { get; set; } 
    public virtual Customer Customer { get; set; } 

    [MaxLength(50)] 
    public string UserName { get; set; } //get user name from the aspnet membership 

    [Required(ErrorMessage = "Case Category is Required")] 
    public int CaseCategoryID { get; set; } 

    [Required(ErrorMessage = "Technician is Required")] 
    public int TechnicianID { get; set; } 
    public virtual Technician Technicians { get; set; } 

    [Required(ErrorMessage = "Engine Model is Required")] 
    public int EngineModelID { get; set; } 
    public virtual EngineModel EngineModel { get; set; } 

    [MaxLength(50)] 
    public string BMSWorkorder { get; set; } 

    [MaxLength(50)] 
    [Required(ErrorMessage = "Status is Required")] 
    public string CaseStatus { get; set; } 

    [MaxLength(50)] 
    public string OpenedBy { get; set; } 

    [Required(ErrorMessage = "Opened Date is Required")] 
    [DataType(DataType.DateTime)] 
    public DateTime? OpenedDate { get; set; } 

    [MaxLength(50)] 
    public string ClosedBy { get; set; } 

    [DataType(DataType.DateTime)] 
    public DateTime? ClosedDate { get; set; } 

    [MaxLength(50)] 
    [Required(ErrorMessage="Caller First Name is Required")] 
    public string CallerFirstName { get; set; } 

    [MaxLength(50)] 
    [Required(ErrorMessage = "Caller Last Name is Required")] 
    public string CallerLastName { get; set; } 

    [MaxLength(100)] 
    public string AdditionalContact { get; set; } 

    [MaxLength(10)] 
    [Required(ErrorMessage = "Qualified is Required")] 
    public string Qualified { get; set; } 

    public string Description { get; set; } 

    [MaxLength(50)] 
    [Required(ErrorMessage = "ESN is Required")] 
    public string ESN { get; set; } 

    [MaxLength(50)] 
    [Required(ErrorMessage = "Mileage is Required")] 
    public string Mileage { get; set; } 

    [DataType(DataType.Date)] 
    public DateTime? DateInService { get; set; } 

    [MaxLength(50)] 
    public string ESTR { get; set; } 

    [MaxLength(50)] 
    [Required(ErrorMessage = "EDS is Required")] 
    public string EDS { get; set; } 

    [MaxLength(50)] 
    public string GensetSerialNumber { get; set; } 

    [MaxLength(50)] 
    public string GensetModelNumber { get; set; } 

    //child Case Notes records 
    public virtual ICollection<CaseNotes> CaseNotes { get; set; } 

    //child case attachment records 
    public virtual ICollection<Attachment> Attachments { get; set; } 

    //child case complaint records 
    public virtual ICollection<CaseComplaint> CaseComplaint { get; set; } 

    //tracking fields 
    public DateTime? CreatedOn { get; set; } 
    [MaxLength(50)] 
    public string CreatedBy { get; set; } 
    public DateTime? ModifiedOn { get; set; } 
    [MaxLength(50)] 
    public string ModifiedBy { get; set; } 
    } 

我很奇怪,爲什麼即使只有一些屬性被標記需要,ModelState中不被設置有效,除非所有屬性都保存時的值。

我做錯了什麼?

編輯

這裏是我的下拉列表場剃刀元素問題:

@Html.DropDownList("Qualified", String.Empty) 
@Html.ValidationMessageFor(model => model.Qualified) 

@Html.DropDownList("EngineModelID", String.Empty) 
@Html.ValidationMessageFor(model => model.EngineModelID) 

@Html.DropDownList("CaseCategoryID", String.Empty) 
@Html.ValidationMessageFor(model => model.CaseCategoryID) 
+0

特別是哪些屬性? – 2012-07-18 13:44:38

+0

其中一些整數屬性應該可以爲空。 – 2012-07-18 13:45:22

+0

我注意到一些斷點,我的驗證註釋不適用於下拉列表視圖字段。任何詭計給那些? – Ryan 2012-07-18 14:10:42

回答

2

EngineModelIDCaseCategoryID性能必須對您的視圖模型爲空的整數,如果你想允許空值。哦,你沒有使用視圖模型。

ASP.NET MVC自動生成所需的非空類型。你可以在你的Application_Start明確禁用此:

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; 

但是如果你想要做的事情正確,你應該使用視圖模型。

下是絕對可怕:

@Html.DropDownList("CaseCategoryID", String.Empty) 

我猜你已經塞在一個ViewBag.CaseCategoryID一個SelectList所以CaseCategoryID做兩件事情在同一時間:它代表一個列表和選擇的標量值。

隨着視圖模型,你會使用這些傭工的強類型版本:

@Html.DropDownListFor(x => x.CaseCategoryID, Model.CaseCategories) 

其中CaseCategories將是一個IEnumerable<SelectListItem>財產上的視圖模型,該控制器將填充。

+0

是的,我的意圖是將此視圖改爲使用視圖模型,但我仍在學習。看起來大多數教程都在講授「可怕」的練習...... :) 我其實需要EngineModelID和CaseCategoryID。 – Ryan 2012-07-18 14:47:31

+0

我想有無論如何得到這個驗證工作,所以我可以讓我的團隊生活的視圖,而我將其更改爲視圖模型? – Ryan 2012-07-18 14:52:36

+0

你嘗試設置DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes爲false嗎? – 2012-07-18 14:54:38