2012-11-22 39 views
2

我想知道什麼是正確的方法來解決這個問題。我目前有一個模型 - (如下所示),其中包含我的記錄所需的所有字段。一個模型只更新每個視圖中的一些字段

我的問題是,當創建記錄時,我只需要傳遞 CustomerID,EmployeeID,Date和ArrivalTime的數據。

當記錄在稍後階段更新時,模型中剩餘的字段將被填充。

由於我的一些領域是必需的,如果我不發佈這些領域的數據,這顯然會導致驗證錯誤。

我在想什麼是實現這一目標的最佳實踐?

我應該將模型分成兩個?還是可以進行部分驗證?

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

    [Required(ErrorMessage = "Please select a Departure Time")] 
    [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; } 

    [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; } 
} 
+0

是'CustomerSupportRecord'域之一模型還是視圖模型? – HTX9

+0

這是一個域模型。 – Stephen

回答

1

正確的做法是對不同的視圖使用不同的viewmodel類,並且只包含在該視圖中需要的屬性。

所以你的第一個視圖視圖模型看起來就像這樣:

public class CustomerSupportRecordForCreation 
{ 
    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; } 
} 

你必須是視圖模型類和您的域名/ DAL類之間的映射。這就是像AutoMapper這樣的工具派上用場的地方。

編輯 Automapper:

使用Automapper是非常簡單的。

  1. 您必須配置您的映射(即在Application_Start)。當要映射的類的屬性同名,它的這樣簡單:

    Mapper.CreateMap<CustomerSupportRecord, 
            CustomerSupportRecordForCreation>(); 
    
  2. 然後你可以使用你的應用程序映射。當你有一個CustomerSupportRecord並希望返回CustomerSupportRecordForCreation對你的看法寫:

    CustomerSupportRecord record = getRecordFromDb... 
    return View(Mapper.Map<CustomerSupportRecordForCreation>(record)); 
    

在CodeProject上一個很好的教程文章:http://www.codeproject.com/Articles/61629/AutoMapper或只是google

+0

謝謝@Jan我以前沒有使用過Auto Mapper。提供如何做到以上的快速樣本的任何機會,或者你可以指點我一個很好的教程? – Stephen

+0

@Stephen看我的編輯 – Jan

相關問題