<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識(shí)百科 - 正文

        Asp.net MVC 對(duì)所有用戶輸入的字符串字段做Trim處理的方法

        來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:35:30
        文檔

        Asp.net MVC 對(duì)所有用戶輸入的字符串字段做Trim處理的方法

        Asp.net MVC 對(duì)所有用戶輸入的字符串字段做Trim處理的方法:經(jīng)常需要對(duì)用戶輸入的數(shù)據(jù)在插入數(shù)據(jù)庫或者判斷之前做Trim處理,針對(duì)每個(gè)ViewModel的字段各自做處理是我們一般的想法。最近調(diào)查發(fā)現(xiàn)其實(shí)也可以一次性實(shí)現(xiàn)的。 MVC4.6中實(shí)現(xiàn)方式 1,實(shí)現(xiàn)IModelBinder接口,創(chuàng)建自定義ModelBinder。 public c
        推薦度:
        導(dǎo)讀Asp.net MVC 對(duì)所有用戶輸入的字符串字段做Trim處理的方法:經(jīng)常需要對(duì)用戶輸入的數(shù)據(jù)在插入數(shù)據(jù)庫或者判斷之前做Trim處理,針對(duì)每個(gè)ViewModel的字段各自做處理是我們一般的想法。最近調(diào)查發(fā)現(xiàn)其實(shí)也可以一次性實(shí)現(xiàn)的。 MVC4.6中實(shí)現(xiàn)方式 1,實(shí)現(xiàn)IModelBinder接口,創(chuàng)建自定義ModelBinder。 public c

        經(jīng)常需要對(duì)用戶輸入的數(shù)據(jù)在插入數(shù)據(jù)庫或者判斷之前做Trim處理,針對(duì)每個(gè)ViewModel的字段各自做處理是我們一般的想法。最近調(diào)查發(fā)現(xiàn)其實(shí)也可以一次性實(shí)現(xiàn)的。

        MVC4.6中實(shí)現(xiàn)方式

        1,實(shí)現(xiàn)IModelBinder接口,創(chuàng)建自定義ModelBinder。

        public class TrimModelBinder : IModelBinder
         {
         public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
         {
         var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
         string attemptedValue = valueResult?.AttemptedValue;
        
         return string.IsNullOrWhiteSpace(attemptedValue) ? attemptedValue : attemptedValue.Trim();
         }
         }

        2,添加ModelBinder到MVC的綁定庫。

        protected void Application_Start()
         {
         //System.Web.Mvc.ModelBinders.Binders.DefaultBinder = new ModelBinders.TrimModelBinder();
         System.Web.Mvc.ModelBinders.Binders.Add(typeof(string), new ModelBinders.TrimModelBinder());
         AreaRegistration.RegisterAllAreas();
         FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
         RouteConfig.RegisterRoutes(RouteTable.Routes);
         BundleConfig.RegisterBundles(BundleTable.Bundles);
         }

        3,確認(rèn)一下效果

        將密碼后面的空格做Trim處理,綁定到ViewModel的時(shí)候變成1了:

        Asp.net core 1.1 MVC中實(shí)現(xiàn)方式

        1,自定義ModelBinder并繼承ComplexTypeModelBinder

        public class TrimModelBinder : ComplexTypeModelBinder
         {
         public TrimModelBinder(IDictionary propertyBinders) : base(propertyBinders) { }
         
         protected override void SetProperty(ModelBindingContext bindingContext, string modelName, ModelMetadata propertyMetadata, ModelBindingResult result)
         {
         var value = result.Model as string;
         
         result= string.IsNullOrWhiteSpace(value) ? result : ModelBindingResult.Success(value.Trim());
         
         base.SetProperty(bindingContext, modelName, propertyMetadata, result);
         }
         }

        2,為ModelBinder添加自定義Provider

        public class TrimModelBinderProvider : IModelBinderProvider
         {
         public IModelBinder GetBinder(ModelBinderProviderContext context)
         {
         if (context.Metadata.IsComplexType && !context.Metadata.IsCollectionType)
         {
         var propertyBinders = new Dictionary();
         for (int i = 0; i < context.Metadata.Properties.Count; i++)
         {
         var property = context.Metadata.Properties[i];
         propertyBinders.Add(property, context.CreateBinder(property));
         }
         return new TrimModelBinder(propertyBinders);
         }
         return null;
         }
         }

        3,將Provider添加到綁定管理庫

        services.AddMvc().AddMvcOptions(s =>
         {
         s.ModelBinderProviders[s.ModelBinderProviders.TakeWhile(p => !(p is ComplexTypeModelBinderProvider)).Count()] = new TrimModelBinderProvider();
         });

        4,確認(rèn)一下效果

        將密碼后面的空格做Trim處理,綁定到ViewModel的時(shí)候變成1了:

        以上所述是小編給大家介紹的Asp.net MVC 對(duì)所有用戶輸入的字符串字段做Trim處理的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        Asp.net MVC 對(duì)所有用戶輸入的字符串字段做Trim處理的方法

        Asp.net MVC 對(duì)所有用戶輸入的字符串字段做Trim處理的方法:經(jīng)常需要對(duì)用戶輸入的數(shù)據(jù)在插入數(shù)據(jù)庫或者判斷之前做Trim處理,針對(duì)每個(gè)ViewModel的字段各自做處理是我們一般的想法。最近調(diào)查發(fā)現(xiàn)其實(shí)也可以一次性實(shí)現(xiàn)的。 MVC4.6中實(shí)現(xiàn)方式 1,實(shí)現(xiàn)IModelBinder接口,創(chuàng)建自定義ModelBinder。 public c
        推薦度:
        標(biāo)簽: mv net 字符串
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 午夜免费福利网站| 无人在线观看完整免费版视频| 国产免费人人看大香伊| 国产成人精品日本亚洲直接| 在线看片v免费观看视频777 | 特级毛片免费播放| 四虎永久免费影院在线| 国产精品亚洲五月天高清| 亚洲第一福利网站在线观看| 一级毛片免费毛片毛片| 亚洲男人的天堂www| 一级毛片全部免费播放| 亚洲国产综合在线| 日本xxwwxxww在线视频免费| 国产精品日本亚洲777| 久久亚洲中文字幕精品一区四| 七次郎成人免费线路视频| 日本红怡院亚洲红怡院最新| 久久精品无码专区免费东京热| 亚洲国产av美女网站| 欧洲精品免费一区二区三区| 四虎国产精品永免费| 亚洲欧洲精品无码AV| 99久久久国产精品免费蜜臀| 日韩亚洲产在线观看| 亚洲午夜激情视频| 114级毛片免费观看| 色偷偷尼玛图亚洲综合| 亚洲午夜久久久影院| 巨波霸乳在线永久免费视频 | 浮力影院亚洲国产第一页| 久久久精品午夜免费不卡| 亚洲人成电影院在线观看| 免费成人黄色大片| 午夜免费啪视频在线观看 | 亚洲一区免费观看| 亚洲砖码砖专无区2023| 亚洲一级特黄无码片| 亚洲免费电影网站| 一区二区三区免费看| 亚洲av成人一区二区三区|