gpt4 book ai didi

c# - “比较”是 'System.ComponentModel.DataAnnotations.CompareAttribute' 和 'System.Web.Mvc.CompareAttribute' 之间的模糊引用

转载 作者:可可西里 更新时间:2023-11-01 08:35:36 25 4
gpt4 key购买 nike

我的 AccountController 中有这个错误。

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

明显的解决方法是添加 using System.Web.Mvc; 但是当我这样做时我得到 4 个新错误

在两条不同的线上:

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

另外两条不同的线:

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

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

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


}

重置密码 View 模型

public class ResetPasswordViewModel
{

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

}

最佳答案

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

根据 msdn documentation , System.Web.Mvc.CompareAttribute 已过时,推荐使用System.ComponentModel.DataAnnotations.CompareAttribute

因此要么使用包括命名空间的完全限定名称。

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

或者,如果您不想在所有地方都使用完全限定名称,则可以使用别名

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
}

关于c# - “比较”是 'System.ComponentModel.DataAnnotations.CompareAttribute' 和 'System.Web.Mvc.CompareAttribute' 之间的模糊引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36318044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com