'Compare'是'System.ComponentModel.DataAnnotations.CompareAttribute'和'System.Web.Mvc.CompareAttribute'之间的模糊参考 [英] 'Compare' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.CompareAttribute' and 'System.Web.Mvc.CompareAttribute'

查看:328
本文介绍了'Compare'是'System.ComponentModel.DataAnnotations.CompareAttribute'和'System.Web.Mvc.CompareAttribute'之间的模糊参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的AccountController中有这个错误。

I have this error in my AccountController .


找不到类型或命名空间名称SelectListItem(您是否缺少using指令或程序集引用?

The type or namespace name 'SelectListItem' could not be found ( are you missing a using directive or an assembly reference?

明显的修复是使用System.Web.Mvc; 添加但是当我做得到4个新错误

The obvious fix is to add using System.Web.Mvc; However when I do I get 4 new errors

两条不同的行:


类型或命名空间找不到名称'ErrorMessage'(您是否缺少using指令或程序集引用?)

The type or namespace name 'ErrorMessage' could not be found (are you missing a using directive or an assembly reference?)

另外两行不同:


'比较'是'System.ComponentModel.DataAnnotations.CompareAttribute'和'System.Web.Mvc.CompareAttribute'

'Compare' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.CompareAttribute' and 'System.Web.Mvc.CompareAttribute'

为什么会发生这种情况,我该如何解决?

Why does this happen and how do I fix it?

public class RegisterViewModel
    {
[DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
       public IEnumerable<SelectListItem> DepotList { get; set; }


}

ResetPasswordViewModel

ResetPasswordViewModel

public class ResetPasswordViewModel
{

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]

}


推荐答案

是的。这两个命名空间的属性具有相同的功能。

Yea. Both of those namespaces has that attribute which has same functionality.

根据 msdn文档 System.Web.Mvc.CompareAttribute 已过时,它建议使用 System.ComponentModel.DataAnnotations.CompareAttribute

As per the msdn documentation, System.Web.Mvc.CompareAttribute is obsolete and it is recommended to use System.ComponentModel.DataAnnotations.CompareAttribute

所以要么使用包含命名空间的完全限定名称。

So either use the fully qualified name including the namespace.

[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[System.ComponentModel.DataAnnotations.Compare("Password",
                    ErrorMessage = "The password and confirmation password do not match.")]
public string Name { get; set; }

或者你可以使用别名,如果你不想把完全限定名称全部使用Compare = System.ComponentModel.DataAnnotations.CompareAttribute的地方

Or you can use an alias name if you do not want to put the fully qualified name in all the places

using Compare = System.ComponentModel.DataAnnotations.CompareAttribute;
public class ResetPasswordViewModel
{
   [DataType(DataType.Password)]   
   [Compare("Password", ErrorMessage = "The password and confirm password do not match.")]
   public string Password { set;get;}
   //Other properties as needed
}

这篇关于'Compare'是'System.ComponentModel.DataAnnotations.CompareAttribute'和'System.Web.Mvc.CompareAttribute'之间的模糊参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆