1

我正在尝试仅使用我的域模型中的一些字段来创建新记录。我为此创建了一个 ViewModel,并且正在使用 AutoMapper。

由于我的域模型上的必填字段,我的代码现在失败了。我在必要的字段上添加了 opt=>opt.Ignore() 但是,我仍然遇到问题。

当我从 StaffPresent 字段中删除 [Required] 时,记录被添加到数据库中。

在我的 Global.asax.cs

Mapper.CreateMap<CustomerSupportRecord, CustomerSupportRecordForCreation>();
Mapper.CreateMap<CustomerSupportRecordForCreation, CustomerSupportRecord>().ForMember(p=>p.StaffPresent, opt=>opt.Ignore());

领域模型

public class CustomerSupportRecord
{
    public int CustomerSupportRecordID { get; set; }

    [Required]
    public int CustomerID { get; set; }

    [Required]
    public string EmployeeID { get; set; }

    [Required(ErrorMessage = "Please enter a Date")]
    [DataType(DataType.Date)]
    [Display(Name = "Date")]
    public DateTime Date { get; set; }

    [Required(ErrorMessage = "Please select an Arrival Time")]
    [DataType(DataType.Time)]
    [Display(Name = "Arrival")]
    public DateTime ArrivalTime { get; set; }

    [DataType(DataType.Time)]
    [Display(Name = "Departure")]
    public DateTime? DepartureTime { get; set; }

    [Required(ErrorMessage = "Please select a Type")]
    [Display(Name = "Type")]
    public int CustomerSupportTypeID { get; set; }

    [Required(ErrorMessage = "Please enter the staff who were present at the Feedback")]
    [Display(Name = "Staff Present at Feedback")]
    public string StaffPresent { get; set; }

    [Display(Name = "Setting")]
    public string ReflectionSetting { get; set; }

    [Display(Name = "Advisor")]
    public string ReflectionAdvisor { get; set; }

    [Display(Name = "Notes")]
    public string Notes { get; set; }

    [Display(Name = "Comments")]
    public string Comments { get; set; }

    // Navigation Properties
    public virtual Customer Customer { get; set; }
    public virtual CustomerSupportType CustomerSupportType { get; set; }
    public virtual Employee Employee { get; set; }
}

视图模型

public class CustomerSupportRecordForCreation
{
    public int CustomerSupportRecordID { get; set; }

    public int CustomerID { get; set; }

    public string EmployeeID { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Date")]
    public DateTime Date { get; set; }

    [DataType(DataType.Time)]
    [Display(Name = "Arrival")]
    public DateTime ArrivalTime { get; set; }

    [Display(Name = "Type")]
    public int CustomerSupportTypeID { get; set; }

    [Display(Name = "Notes")]
    public string Notes { get; set; }
}

最后是我的控制器

    //
    // GET: /CustomerSupport/CustomerSupportRecord/Create

    public ActionResult Create()
    {
        ViewBag.CustomerSupportTypeID = new SelectList(db.CustomerSupportType, "CustomerSupportTypeID", "CustomerSupportTypeName");
        var model = new CustomerSupportRecordForCreation { CustomerID = 1, EmployeeID = "20213" };

        return View("Create", model);

    }

    //
    // POST: /CustomerSupport/CustomerSupportRecord/Create

    [HttpPost]
    public ActionResult Create(CustomerSupportRecordForCreation customersupportrecord)
    {

        if (ModelState.IsValid)
        {
            var newRecord = Mapper.Map<CustomerSupportRecordForCreation, CustomerSupportRecord>(customersupportrecord);
            db.CustomerSupportRecord.Add(newRecord);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
            ViewBag.CustomerSupportTypeID = new SelectList(db.CustomerSupportType, "CustomerSupportTypeID", "CustomerSupportTypeName", customersupportrecord.CustomerSupportTypeID);
            return View(customersupportrecord);
    }
4

1 回答 1

2

AutoMapper 的配置与验证属性没有任何关系,它只是指定了如何在彼此之间映射对象的规则。

在您的情况下,映射:

Mapper.CreateMap<CustomerSupportRecordForCreation, CustomerSupportRecord>().ForMember(p=>p.StaffPresent, opt=>opt.Ignore());

告诉 AutoMapper 不要复制 StaffPresent 属性。

如果您的数据库模型具有 [Required] 属性,您仍然需要在其他地方设置该数据。

根据您的评论需要注意的一件事是,向您的 ViewModel 添加验证是一个好主意,因为它可以防止您使用不正确的数据调用您的数据库方法,为您提供客户端验证,并允许您强制执行不同于模型可能需要的约束.

于 2012-11-26T18:15:49.307 回答