I have the following scenario: a basic C # project with all data domains (custom types) that are used by all other projects in the company. So change it a bit.
Now we are creating our first mvc project with the base project as a reference, and model binding does not work for the properties of these types of user types:
[Serializable] [TypeConverter(typeof(ShortStrOraTypeConverter))] public class ShortStrOra : BaseString { public ShortStrOra() : this(String.Empty) { } public ShortStrOra(string stringValue) : base(stringValue, 35) { } public static implicit operator ShortStrOra(string stringValue) { return new ShortStrOra(stringValue); } public static implicit operator string(ShortStrOra value) { if (value == null) { return null; } else { return value.ToString(); } } public void Add(object o) { } }
TypeConverter:
public class ShortStrOraTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) return true; return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value is string) return new ShortStrOra(Convert.ToString(value)); return base.ConvertFrom(context, culture, value); } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string)) return ((ShortStrOra)value).ToString(); return base.ConvertTo(context, culture, value, destinationType); } }
And in my simple test, with this single class, the Name property was not bound, but Lastname has.
public class TesteModel { public ShortStrOra Name {get; set;} public String Lastname { get; set; } public TesteModel() { } }
My opinion:
@using (Html.BeginForm("EditMember", "Home", FormMethod.Post, new { @id = "frmEditMembers" })) { @Html.TextBoxFor(m => m.Name)<br /> @Html.TextBoxFor(m => m.Lastname) <input type="submit" value="Salvar" /> }
My controller:
public ActionResult EditMember(TesteModel model) { return View("Index", model); }
Finally, where is the problem? Serialization? Binding model? Converter? I do not know where to go. No errors or exceptions.
Any ideas? Thanks