2
votes

I am getting this strange issue in MVC 3.

I have a strongly typed view and a controller which has a SaveMethod(Employee emp).

But when i click SAVE, it is not hitting Save method and throws below error.

I think there may be a model binder issue. But how i know which property is a problem?

Its difficult for me to find a property from 50

Exception:

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

Source: System.ComponentModel.DataAnnotations

StackTrace

at System.ComponentModel.DataAnnotations.StringLengthAttribute.IsValid(Object value) at System.ComponentModel.DataAnnotations.ValidationAttribute.IsValid(Object value, ValidationContext validationContext) at System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(Object value, ValidationContext validationContext) at System.Web.Mvc.DataAnnotationsModelValidator.d_1.MoveNext() at System.Web.Mvc.ModelValidator.CompositeModelValidator.d_5.MoveNext() at System.Web.Mvc.DefaultModelBinder.OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) at System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) at System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) at System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

How do i check which property causes this issue?

2
show your code. and the error is clearly descriptive. - Ravi Gadag
Post your controller action method - graumanoz
Show your controller code and your view model as well. We will help you to investigating this issue - thangchung

2 Answers

9
votes

Looks to me like you've applied a [StringLength] to an integer property on your model:

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

That will throw the kind of error that you're seeing.

3
votes

Look in your model if you have the StringLength attribute applied to a property of type int.