System.ComponentModel.DataAnnotations.compare vs System.Web.Mvc.Compare

前端 未结 6 1944
星月不相逢
星月不相逢 2020-12-24 04:44

MVC 4 Beta project fails to compile after upgrading to .Net 4.5.

This happens due to conflict between System.ComponentModel.DataAnnotations.CompareAttribute

6条回答
  •  甜味超标
    2020-12-24 05:34

    So, looking at the MSDN documentation and doing a literal comparison of the two classes, I noticed both classes are derived from System.ComponentModel.DataAnnotations.ValidationAttribute. In fact, the classes are almost exactly the same. The only notable difference is that the MVC version also implements IClientValidatable which adds the following properties:

    • FormatPropertyForClientValidation - (static member) Formats the property for client validation by prepending an asterisk and a dot.
    • GetClientValidationRules - Gets a list of compare-value client validation rules for the property using the specified model metadata and controller context.

    As for which class you should use, if the model will be directly bound to a view, use the MVC version so that you can take advantage of the client-side validation. However, if you're using ViewModels, you can stick with the ComponentModel class and avoid the unnecessary overhead of the additional properties. Your call!

    • System.Web.Mvc.CompareAttribute

    • System.ComponentModel.DataAnnotations.CompareAttribute

提交回复
热议问题