无效的转换异常,甚至当我已经定义了隐式转换操作符(在ASP MVC应用程序)
-
22-09-2019 - |
题
我有一个MVC模式类创建和性质中的一种类型是“为MyObject”的。它也有在其上的System.ComponentModel.DataAnnotations.StringLength属性。
MyObject的隐式转换运算符,因此它可以基本上被用作字符串:
public static implicit operator string(MyObject o){...}
public static implicit operator MyObject(string sValue){...}
这是一些奇怪的原因一个ASP MVC问题?我问,因为我知道,在大多数情况下,隐式转换工作正常,我可以为例如指定该属性设置为一个字符串值,并将它工作得很好。
编辑 - 好吧,我知道为什么错误是发生:点击 这是因为StringLength.IsValid()方法需要一个对象作为参数,所以中投实际上是从对象去字符串,而不是从MyObject的串,所以这可以解释为什么我的隐式转换操作符不会被调用。但如何解决此问题?
这一切工作正常,直到我把财产的System.ComponentModel.DataAnnotations.StringLength属性在我的模型,然后在视图做一个职位从一个提交按钮,我得到了异常:
[InvalidCastException的:无法投 种类型的目标 “StrataSpot.Shared.Models.Email”来 输入 '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的 的PropertyDescriptor,对象的值)203 System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext,ModelBindingContext BindingContext中,PropertyDescriptor的 PropertyDescriptor的)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,字符串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 步骤,布尔逻辑completedSynchronously) 155
解决方案
您无法使用[StringLength]比字符串以外的类型的属性。如果你要复制的功能,你可以继承StringLengthAttribute:
public class MyCoolAttribute : StringLengthAttribute {
// constructor here
public override bool IsValid(object value) {
return base.IsValid((string)(value as MyObject));
}
}
然后拍击[MyCool]而非[StringLength]你的财产。在这方面使用强制运营商可能不是世界上最干净的东西;你应该使用的ToString()或者类似的东西。但这个想法是相同的。
另外,如果你不想继承StringLengthAttribute,你可以委托给一个私人StringLengthAttribute实例的isValid()方法。