2011-11-08 166 views
1

我有一個名爲組織模式,對組織我有一個遠程驗證屬性:ASP.NET MVC 3模型驗證

[Required(ErrorMessage = "The organisation name is required")] 
    [Remote("NameCheck", "Manage", "Organisations", ErrorMessage="That organisation  already exists")] 
    public string Name { get; set; } 

這就驗證了該組織的人的名字將不會已經存在。如果確實如此,他們會收到一條錯誤消息。

我使用強類型視圖來呈現組織「編輯」視圖。因爲有人正在編輯,我不希望遠程驗證運行,因爲組織將存在。

有什麼辦法可以達到這個目的嗎?基本上,在編輯組織時以某種方式關閉遠程驗證,並在創建組織時將其打開。

+4

如果他的用戶期望英國英語,那麼它將是組織。如果他們期望美國英語,那麼它將是組織。從他一貫的拼寫,我期望它是前者。 –

+0

John Hartsock,Jared Peless,這樣的評論可能對http://english.stackexchange.com/很有用,但在編程相關的問答站點(如StackOverflow)中並不是很有用。 –

回答

3
public class BaseOrganizationModel { 
    public int ID {get; set;} 
} 

public class UpdateOrganizationModel : BaseOrganizationModel { 
    [Required(ErrorMessage = "The organisation name is required")] 
    public string Name { get; set; } 

} 

public class InsertOrganizationModel : BaseOrganizationModel { 
    [Required(ErrorMessage = "The organisation name is required")] 
    [Remote("NameCheck", "Manage", "Organisations", ErrorMessage="That organisation  already exists")] 
    public string Name { get; set; } 

} 
+0

謝謝,回答這個問題。 – Paul

4

你可以/ 應該使用不同的視圖模型的兩個視圖。因此,例如,您將擁有CreateOrganizationViewModel和UpdateOrganizationViewModel。在第一個視圖模型中,Name屬性將使用遠程屬性進行修飾,而在第二個視圖模型中則不會。

+0

謝謝。好答案。 – Paul