0

使用这个模型:

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; }
    }

我想知道为什么即使只有一些属性被标记为必需,除非所有属性在保存时都有值,否则模型状态不会设置为有效。

难道我做错了什么?

编辑

以下是相关下拉列表字段的剃刀元素:

@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)
4

1 回答 1

2

如果您想允许空值,则 and 属性在您的视图模型上必须是可为空的整数EngineModelIDCaseCategoryID糟糕,您没有使用视图模型。

ASP.NET MVC 自动使不可为空的类型成为必需。您可以在您的Application_Start

DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;

但是如果你想正确地做这些事情,你应该使用视图模型。

以下是绝对可怕的:

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

我猜你已经把 a 塞进了SelectListaViewBag.CaseCategoryID所以它CaseCategoryID同时做两件事:它代表一个列表和一个选定的标量值。

对于视图模型,您将使用这些助手的强类型版本:

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

whereCaseCategories将是IEnumerable<SelectListItem>控制器将填充的视图模型上的属性。

于 2012-07-18T14:41:10.270 回答