如何设置默认值在asp.net MVC应用程序的模型空字符串 [英] How to set Default value as Empty string in Model in asp.net mvc application
本文介绍了如何设置默认值在asp.net MVC应用程序的模型空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么办法可以我设置默认值Empty.string的模型。
Is there any way that can I set default value as Empty.string in Model.
我有一个列名其与默认值的数据库NOT NULL字段Empty.string模型
I have a column Name in the Model its not null field in the database with default value is Empty.string
在那里,我可以在模型中设置此默认属性此列任何方式?
is there any way that I can set this default property in the Model for this column?
感谢
推荐答案
有对这个您可以通过覆盖默认的模型绑定配置设置如下:
There is a setting for this which you can configure by overriding the default model binder as follows:
public sealed class EmptyStringModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
bindingContext.ModelMetadata.ConvertEmptyStringToNull = false;
return base.BindModel(controllerContext, bindingContext);
}
}
然后配置本作中,应用程序启动默认的模型绑定在Global.asax:
then configure this as the default model binder in application start in the global.asax:
ModelBinders.Binders.DefaultBinder = new EmptyStringModelBinder();
和你去那里,没有更多的空字符串。
and there you go, no more null strings.
这篇关于如何设置默认值在asp.net MVC应用程序的模型空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文