asp.net - c# - Custom type Convert.ToString -
i'm having trouble when rendering text box using @html.textboxfor custom type i've created. custom type looks this:
public class encrypted<t> { private readonly lazy<t> _decrypted; private readonly lazy<string> _encrypted; public static implicit operator encrypted<t>(t value) { return new encrypted<t>(value); } public static implicit operator string(encrypted<t> value) { return value._encrypted.value; } ... } then on model, have:
public class examplemodel { public encrypted<string> name { get; set; } } if manually populate value in controller action:
public actionresult index() { var model = new examplemodel { name = "example name"; }; return view(model); } then on view have standard @html.textboxfor(m => m.name). however, when renders, value of text box gets set to: services.encrypted`1[system.string]`
presumably because i'm using custom type , compiler doesn't know how convert type string value.
i've tried using custom typeconverter:
public override bool canconvertto(itypedescriptorcontext context, type destinationtype) { return destinationtype == typeof(string); } public override object convertto(itypedescriptorcontext context, system.globalization.cultureinfo culture, object value, type destinationtype) { if (destinationtype == typeof(string)) { var encrypted = value iencrypted; if (encrypted != null) { return encrypted.decryptedvalue(); } } return null; } then on encrypted model added:
[typeconverter(typeof(encryptedtypeconveter))] however doesn't seem using custom typeconverter. know how can resolve this?
you need override tostring().
Comments
Post a Comment