c# - MVC3 模型绑定(bind)问题

标签 c# asp.net asp.net-mvc asp.net-mvc-3

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

我有一个强类型 View 和一个具有 SaveMethod(Employee emp) 的 Controller 。

但是当我点击保存时,它没有点击保存方法并抛出以下错误。

我认为可能存在模型 Binder 问题。但是我怎么知道哪个属性有问题呢?

我很难从50 block 找到一个属性

异常:

无法将“System.Int32”类型的对象转换为类型“System.String”。

来源:System.ComponentModel.DataAnnotations

堆栈跟踪

在 System.ComponentModel.DataAnnotations.StringLengthAttribute.IsValid(对象值) 在 System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(对象值,ValidationContext validationContext) 在 System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(对象值,ValidationContext validationContext) 在 System.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.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext,ModelBindingContext bindingContext,对象模型) 在 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext,ModelBindingContext bindingContext) 在 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext) 在 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext,ParameterDescriptor parameterDescriptor) 在 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext,ActionDescriptor actionDescriptor) 在 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

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

最佳答案

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

public class MyModel {
  [StringLength]
  public int SomeValue { get; set; }
}

这将引发您所看到的那种错误。

关于c# - MVC3 模型绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16519737/

相关文章:

c# - 如何在 asp.net 中的 Handler 中设置 session 值

c# - 哪个运行时性能更快 : WPF or Winforms?

ASP.Net WebForms 或 MVC 哪一个适合我作为 .Net 新手?

c# - 在不将文件保存到磁盘的情况下读取多部分表单数据?

javascript - 如何在 ASP.NET MVC 中重置 <input type ='file'/>

c# - 如何在 AuthorizeAttribute ASP.NET Web.API MVC 5 的 IsAuthorized 上获取 Post 参数

c# - 如何在Web服务器上获取用户的IP地址?

c# - 带有鉴别器问题的 MongoDB 反序列化

asp.net - 客户端库或 AJAX 控制工具包? - ASP.NET

asp.net - 在 asp.net mvc 中使用 Ajax 将文件和文本上传到服务器