最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決asp.net mvc UpdateModel更新對象后出現(xiàn)null問題的方法

 更新時間:2015年11月05日 15:50:05   投稿:lijiao  
這篇文章主要介紹了解決asp.net mvc UpdateModel 更新對象后出現(xiàn)null問題的方法,需要的朋友可以參考下

在用asp.net mvc 4.0做項(xiàng)目的時候遇到的這種情況:
情況分析:
“在填寫表單的時候,有一些表單沒有填寫,留空,然后直接post 提交表單,action中用UpdateModel 來更新model,結(jié)果發(fā)現(xiàn)那些沒有填寫的表單字段全部變成null?!?br /> 原因分析:
項(xiàng)目中做了判斷null不能提交更新到數(shù)據(jù)庫中,所以導(dǎo)致一直提交不上去
后來網(wǎng)上查了一下找到了解決辦法,我在這里分享一下,方便以后遇到這種情況的朋友可以方便解決
解決方法:
新建一個類繼承DefaultModelBinder

using System.ComponentModel;
using System.Web.Mvc;
namespace CustomerWebsite.Mvc
{
 public sealed class EmptyStringToNullModelBinder : DefaultModelBinder
 {
  protected override void SetProperty(ControllerContext controllerContext,
   ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value)
  {
   if (value == null && propertyDescriptor.PropertyType == typeof(string))
   {
    value = string.Empty;
   }

   base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
  }
 }
}

然后在Global.asax的Application_Start中替換DefaultModelBinder

ModelBinders.Binders.DefaultBinder = new EmptyStringToNullModelBinder();

這樣問題就可以解決了,小編也嘗試進(jìn)行了操作,結(jié)果成功了,希望也能幫助這方面有困擾的童鞋解決實(shí)際問題。

相關(guān)文章

最新評論

沅陵县| 深水埗区| 龙泉市| 北川| 闻喜县| 江城| 泾阳县| 普安县| 深圳市| 鄂尔多斯市| 乐平市| 鄂温| 彭山县| 手机| 犍为县| 禹城市| 阿合奇县| 千阳县| 六安市| 尉氏县| 云龙县| 南通市| 通辽市| 祁连县| 镇宁| 嘉黎县| 会泽县| 易门县| 涡阳县| 铁岭县| 普格县| 鱼台县| 莆田市| 黑龙江省| 正镶白旗| 垣曲县| 鹿邑县| 杭锦后旗| 沅陵县| 汽车| 利辛县|