Custom type MVC property is optional in my model

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

+4
source share
1 answer

I found him. I read about model user bindings and then solved the problem with this approach:

Create a new class, override the model binding and check if the property has a custom type, and then initializes it.

 public class TesteModelBinder2 : DefaultModelBinder { protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) { NameValueCollection values = controllerContext.HttpContext.Request.Form; if (propertyDescriptor.PropertyType.Equals(typeof(ShortStrOra))) { ShortStrOra value = new ShortStrOra(values[propertyDescriptor.Name]); propertyDescriptor.SetValue(bindingContext.Model, value); return; } else base.BindProperty(controllerContext, bindingContext, propertyDescriptor); } } 

As a result of this problem, this problem was resolved:

 ShortStrOra value = new ShortStrOra(values[propertyDescriptor.Name]); 

Without this, the engine throws a CastException when trying to set a string value in my type ShortStrOra, but it dies silently and the property value is set in the property.

To use a custom communication device in the controller:

 [HttpPost] public ActionResult EditMember([ModelBinder(typeof(TesteModelBinder2))]TesteModel model) { return View("Index", model); } 
+4
source

All Articles