2

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?

3
  • show your code. and the error is clearly descriptive. Commented May 13, 2013 at 10:23
  • Post your controller action method Commented May 13, 2013 at 10:33
  • Show your controller code and your view model as well. We will help you to investigating this issue Commented May 13, 2013 at 10:52

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.