Frage

Ich habe ein mvc-Modell-Klasse erstellt und eine der Eigenschaften ist vom Typ 'MyObject'.Es hat auch ein System.ComponentModel.DataAnnotations.StringLength-Attribut.

MyObject als implizite cast-Operatoren, so kann es im wesentlichen als string verwendet:

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

Dies ist eine asp-mvc-Ausgabe für einige seltsame Grund?Ich Frage, weil ich weiß, in den meisten Fällen die implizite cast gut funktioniert, kann ich zum Beispiel weisen diese Eigenschaft auf eine Zeichenfolge mit dem Wert und es funktioniert nur feine.

Edit - Ok, ich weiß, warum der Fehler passiert:
Es ist, weil der StringLength.IsValid () - Methode nimmt ein Objekt als parameter, so wird der cast eigentlich vor sich geht, von Objekt zu string, nicht von MyObject string, so dass erklärt, warum mein implizite cast-operator ist nicht genannt.Aber wie um dies zu umgehen?

Dies alles funktionierte gut, bis ich das System.ComponentModel.DataAnnotations.StringLength-Attribut, die Eigenschaft in meinem Modell dann, wenn die Sicht nicht ein post von einem submit-button, bekam ich die Ausnahme:

[InvalidCastException:Unable to cast Objekt-Typ 'StrataSpot.Geteilt.Modelle.E-Mail' zu Typ 'System.String'.]
System.ComponentModel.DataAnnotations.StringLengthAttribute.IsValid(Object Wert) +34
System.Web.Mvc.d__1.MoveNext() +56 System.Web.Mvc.DefaultModelBinder.OnPropertyValidated(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, Object value) +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, Objektmodell) +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__DisplayClass81.<BeginSynchronous>b__7(IAsyncResult _) +12 System.Web.Mvc.Async.WrappedAsyncResult1.End() +59 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +44
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult Ergebnis) +7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8678910 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

War es hilfreich?

Lösung

Sie nicht verwenden können, [StringLength] für eine Eigenschaft von einem anderen Typ als String.Wenn Sie möchten, duplizieren Sie die Funktionen, können Sie eine Unterklasse StringLengthAttribute:

public class MyCoolAttribute : StringLengthAttribute {
  // constructor here

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

Dann schlagen [MyCool] anstelle von [StringLength] auf Ihrem Grundstück.Mit einem cast-operator in dieser Hinsicht wahrscheinlich nicht die sauberste Sache der Welt;Sie sollten wahrscheinlich verwenden Sie ToString() oder etwas ähnliches statt.Aber die Idee ist die gleiche.

Alternativ, wenn Sie nicht wollen, um eine Unterklasse StringLengthAttribute, können Sie einfach delegieren, um ein eigenes StringLengthAttribute Instanz IsValid () - Methode statt.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top