Murali Murugesan
Murali Murugesan

Reputation: 22619

MVC3 Model Binding Issue

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?

Upvotes: 2

Views: 963

Answers (2)

Andras Zoltan
Andras Zoltan

Reputation: 42363

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.

Upvotes: 9

Jan
Jan

Reputation: 16038

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

Upvotes: 3

Related Questions