我意识到这个问题有很多问题。但是,我花了很多时间试图追踪这一点后,我才能解决这个问题。
我的模型如下:
public class Guide
{
public int GuideId { get; set; }
public int GuideTypeId { get; set; }
[Required(ErrorMessage = "Please enter a business name")]
[StringLength(80, ErrorMessage="The Business Name cannot exceed 80 characters")]
public string BusinessName { get; set; }
public System.DateTime? ActiveStartDate { get; set; }
public System.DateTime? ActiveEndDate { get; set; }
[DataType(DataType.Url, ErrorMessage="The website url entered is not a valid url")]
[StringLength(80, ErrorMessage = "The Website Url cannot exceed 80 characters")]
public string WebsiteURL { get; set; }
[Required(ErrorMessage = "Please enter a your primary location")]
[StringLength(80, ErrorMessage = "The Primary Location cannot exceed 80 characters")]
public string BaseLocation { get; set; }
[DataType(DataType.EmailAddress, ErrorMessage = "The email address entered is not a valid email address")]
[Required(ErrorMessage="Please enter a contact email address")]
[StringLength(80, ErrorMessage = "The Email Address cannot exceed 80 characters")]
public string EmailAddress { get; set; }
[DataType(DataType.PhoneNumber, ErrorMessage = "The phone number entered is not a valid phone number")]
[StringLength(20, ErrorMessage = "The Phone Number cannot exceed 20 characters")]
public string PhoneNumber { get; set; }
[Required(ErrorMessage = "Please enter a description of your fees")]
public string FeesDescription { get; set; }
public string VehiclesAvailable { get; set; }
public string AdditionalInformation { get; set; }
public string AccountName { get; set; }
public string Countries { get; set; }
public string NearestCities { get; set; }
public string Locations { get; set; }
public byte[] ProfileImage { get; set; }
public bool Approved { get; set; }
public System.DateTimeOffset LastModifiedTime { get; set; }
// These four properties are here so we can display all tour types, durations, languages and currencies in a checkbox layout on the page. These are not
// what the guide actually supports. These just represent all possible values.
public List<TourType> TourTypeList { get; set; }
public List<TourDuration> TourDurationList { get; set; }
public List<TourLanguage> TourLanguageList { get; set; }
public List<TourCurrency> TourCurrencyList { get; set; }
// These four properties represent what the guide does actually support
public virtual List<GuideTourType> GuideTourType { get; set; }
public virtual List<GuideTourDuration> GuideTourDuration { get; set; }
public virtual List<GuideTourLanguage> GuideTourLanguage { get; set; }
public virtual List<GuideTourCurrency> GuideTourCurrency { get; set; }
public bool HasImage
{
get
{
return ProfileImage.Length > 0;
}
}
public virtual GuideType GuideType { get; set; }
}
我找到了一个尝试使用以下绑定代码调试此内容的人:
public class DebugModelBinder : DefaultModelBinder, IModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
Dictionary<string, ModelMetadata> d = new Dictionary<string, ModelMetadata>(StringComparer.OrdinalIgnoreCase);
foreach (var p in bindingContext.ModelMetadata.Properties)
{
var propertyName = p.PropertyName;
try
{
d.Add(propertyName, null);
}
catch (ArgumentException ex)
{
throw new ArgumentException(String.Format("The Item {0} as already been added", propertyName), ex);
}
}
return base.BindModel(controllerContext, bindingContext);
}
}
我能够使这段代码正常工作,但是当执行这行代码时会发生异常:
return base.BindModel(controllerContext, bindingContext);
所以它发生在基类中。
当我在MVC 5页面上的表单上发帖时,会生成此异常。我的C#控制器方法实际上从未被调用过。绑定中发生错误,我不知道两次添加什么属性。我也不知道如何找到答案。
如果您需要更多信息,我很乐意提供。我已经浪费了几个小时了。
答案 0 :(得分:6)
在我的情况下,它发生在实体中的重复属性
public class Employee
{
public int roleID {get; set;}
...
...
public int RoleID {get; set;}
}
当我删除了int Role时,这个错误就消失了。
答案 1 :(得分:0)
检查您的web.config连接字符串和新模型中的连接字符串是否相同...
因此,如果您删除了模型并重新创建,只需在现有的连接字符串中临时更改名称即可。
答案 2 :(得分:0)
解决方案是从您的类或模型中删除重复属性
答案 3 :(得分:0)
尝试TryUpdateModel(myModel,“”,新字符串[] {“索引”,“标题”,“ aDate”})后出现相同的错误
我检查,仔细检查并三重检查了模型控制器,并查看是否有重复项。娜达,没有。
它在前一天才工作。
最后,我退出了Visual Studio,重新启动了计算机并执行了“解决方案”的“清理和重建”。现在,控制器可以完全按预期工作。
希望这可以节省一些时间:)