MVC自定义类型的属性没有在我的模型绑定 [英] MVC Custom type property is not binding in my model
问题描述
我有以下情形:C#基地项目,由该公司所有其他项目使用的所有数据域(自定义类型)。所以,一个汇入作业有点难以修改。
I have the following scenario: A C# base project, with all data domains (custom types) that are used by all others projects in the company. So, It´s a little bit hard to modify it.
现在,我们正在创造我们与基地项目作为参照第一MVC项目和模型绑定不工作的那些字符串自定义类型的属性:
Now, we are creating our first mvc project with that base project as reference, and the model binding doesnt work for properties of those strings custom 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)
{
}
}
该类型转换器:
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 has not been binded, but Lastname has.
public class TesteModel
{
public ShortStrOra Name {get; set;}
public String Lastname { get; set; }
public TesteModel() { }
}
我的观点:
@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" />
}
我的控制器:
public ActionResult EditMember(TesteModel model)
{
return View("Index", model);
}
最后,问题出在哪里?序列化?模型绑定?转换器?我不知道去哪里。没有任何错误或异常。
Finally, where is the problem? Serialization? Model binding? Converter? I dont know where to go. There is no error or exception.
任何ideias?
谢谢
Any ideias? Thanks
推荐答案
我找到了。我读了有关自定义模型绑定,然后解决了我的问题,这个办法:
I found it. I read about custom model binding and then solved my problem with this approach:
创建一个新的类,覆盖模型绑定,并检查是否属性是自定义类型,然后对其进行初始化。
Creating a new class, to override model binding, and checking if the property is of the 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);
}
}
有什么解决的问题是这一行:
What solved the problem was this line:
ShortStrOra value = new ShortStrOra(values[propertyDescriptor.Name]);
如果没有它的时候尝试设置我的ShortStrOra类型的字符串值,发动机抛出一个CastException,但它静静地死了,一个空值设置好的的属性。
Without it the engine throws a CastException when tries to set a string value on my ShortStrOra type, but it dies silently and a null value is setted on the property.
要在控制器使用自定义的模型绑定:
To use the custom model binder in controller:
[HttpPost]
public ActionResult EditMember([ModelBinder(typeof(TesteModelBinder2))]TesteModel model)
{
return View("Index", model);
}
这篇关于MVC自定义类型的属性没有在我的模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!