Invaid cast exception, even if I have an implicit casting operator defined (in asp mvc app)

I have an mvc model class, and one of the properties is of type "MyObject". It also has the System.ComponentModel.DataAnnotations.StringLength attribute.

MyObject as implicit casting operators, so it can essentially be used as a string:

public static implicit operator string(MyObject o){...}
public static implicit operator MyObject(string sValue){...}

Is this an asp mvc problem for some strange reason? I ask because I know that in most cases implicit operation works fine, I can, for example, assign this property to a string value, and it works fine.

Edit - Well, I know why the error occurs:
This is because the StringLength.IsValid () method takes an object as a parameter, so the cast actually comes from the object to the string, and not from MyObject to the string, so this explains why my implicit operator statement is not called. But how to get around this?

All this worked fine until I put the System.ComponentModel.DataAnnotations.StringLength attribute on the property in my model, and then when the view makes a message from the submit button, I got an exception:

[InvalidCastException: 'StrataSpot.Shared.Models.Email' type 'System.String'.]
System.ComponentModel.DataAnnotations.StringLengthAttribute.IsValid( ) +34
System.Web.Mvc.d__1.MoveNext() +56 System.Web.Mvc.DefaultModelBinder.OnPropertyValidated(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor Descriptor, ) +203 System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor Descriptor) +413
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +90
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, ) +383
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1048
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +280
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +257
System.Web.Mvc.ControllerActionInvoker.GetParameterValues ​​(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +109
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +314 System.Web.Mvc.Controller.ExecuteCore() +105 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +39
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
System.Web.Mvc <. > C__DisplayClass8.b__4() +34 System.Web.Mvc.Async. < > c__DisplayClass1.b__0() +21 System.Web.Mvc.Async. < > c__DisplayClass8 1.<BeginSynchronous>b__7(IAsyncResult _) +12 System.Web.Mvc.Async.WrappedAsyncResult 1.End() +59 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +44
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult ) +7 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8678910 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean & completedSynchronously) +155

+3
1

[StringLength] , String. , StringLengthAttribute:

public class MyCoolAttribute : StringLengthAttribute {
  // constructor here

  public override bool IsValid(object value) {
    return base.IsValid((string)(value as MyObject));
  }
}

slap [MyCool] [StringLength] . , , ; , , ToString() . .

, StringLengthAttribute, IsValid() StringLengthAttribute.

+5

Source: https://habr.com/ru/post/1727176/


All Articles