MVC3模型绑定问题

Mur*_*san 2 c# asp.net asp.net-mvc asp.net-mvc-3

我在MVC 3中遇到了这个奇怪的问题.

我有一个强类型视图和一个具有SaveMethod(Employee emp)的控制器.

但是当我点击SAVE时,它没有点击Save方法并且抛出以下错误.

我认为可能存在模型绑定器问题.但我怎么知道哪个属性有问题?

我很难找到50岁的房产

例外:

Unable to cast object of type 'System.Int32' to type 'System.String'.

来源:System.ComponentModel.DataAnnotations

堆栈跟踪

在System.ComponentModel.DataAnnotations.StringLengthAttribute.IsValid(对象的值)在System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(对象值,ValidationContext validationContext)在System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(对象值,ValidationContext validationContext)在系统.Web.Mvc.DataAnnotationsModelValidator.d_ 1.MoveNext()在System.Web.Mvc.ModelValidator.CompositeModelValidator.d _5.MoveNext()处于System.Web.Mvc.DefaultModelBinder.OnModelUpdated(ControllerContext controllerContext,ModelBindingContext bindingContext). System.Web.Mvc.DevaultModelBinder.BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)上的System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext,ModelBindingContext bindingContext)中的Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext,ModelBindingContext bindingContext,Object model)在S System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext,String actionName)中的System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext,ActionDescriptor actionDescriptor)中的ystem.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext,ParameterDescriptor parameterDescriptor)

我如何检查哪个属性导致此问题?

And*_*tan 9

在我看来,你已经将[StringLength]应用于模型的整数属性:

public class MyModel {
  [StringLength]
  public int SomeValue { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这会引发你所看到的那种错误.