2013-05-13 120 views
2

我得到這個奇怪的問題在MVC 3MVC3模型綁定問題

我有一個強類型的視圖,並具有SaveMethod(員工EMP)的控制器。

但是,當我點擊保存,它不是擊中保存方法,並拋出錯誤。

我認爲可能存在模型綁定器問題。但我怎麼知道哪個屬性是個問題?

它,我很難找到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) 在System.Web.Mvc.DataAnnotationsModelValidator.d_ 1.MoveNext() 在System.Web.Mvc.ModelValidator.CompositeModelValidator.d _5.MoveNext() 在System.Web.Mvc.DefaultModelBinder.OnModelUpdated( ControllerContext controllerContext,ModelBindingContext bindingContext) at System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerCont分機,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,字符串actionName)

我該如何檢查哪個屬性原因這個問題呢?

+0

顯示您的代碼。而且錯誤顯然是描述性的。 – 2013-05-13 10:23:52

+0

發佈您的控制器操作方法 – graumanoz 2013-05-13 10:33:05

+0

同時顯示您的控制器代碼和視圖模型。我們將幫助您調查此問題 – thangchung 2013-05-13 10:52:39

回答

3

如果將StringLength屬性應用於int類型的屬性,請查看您的模型。

9

看起來對我來說,就像你在你的模型應用了[StringLength]一個整數屬性:

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

這將引發你所看到的那種錯誤。