zoukankan      html  css  js  c++  java
  • MVC集合ModelBinder

    使用腳本提交集合类时,MVC binding

    public class FormExtensionValueProviderFactory : ValueProviderFactory
        {
            public override IValueProvider GetValueProvider(ControllerContext controllerContext)
            {
                NameValueCollection requestData = new NameValueCollection();
                var headers = controllerContext.RequestContext.HttpContext.Request.Headers;
                foreach (string key in headers.Keys)
                {
                    requestData.Add(key.Replace("-", ""), headers[key]);
                }
                var forms = controllerContext.RequestContext.HttpContext.Request.Form;
                foreach (string key in forms.Keys)
                {
                    if (!string.IsNullOrEmpty(key))
                    {
                        requestData.Add(ReplaceText(key), forms[key]);
                    }
                }
                return new NameValueCollectionValueProvider(requestData, CultureInfo.InvariantCulture);
            }
    
            public string ReplaceText(string CommandText)
            {
                Regex reg = new Regex(@"(?<part1>(S*)[)(?<part2>(d*)])(?<part3>([S*]))", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                var result = reg.Match(CommandText).Groups;
    
                if (result["part2"] != null && result.Count > 1)
                {
                    string part1 = result["part1"].Value;
                    string part2 = result["part2"].Value;
                    string part3 = result["part3"].Value;
                    return result["part1"].Value + result["part2"].Value
                         + result["part3"].Value.Replace("[", ".").Replace("]", "");
                }
                else
                {
                    return CommandText;
                }
            }
    
        }

    在 Application_Start 添加

    ValueProviderFactories.Factories.Insert(0, new FormExtensionValueProviderFactory());
  • 相关阅读:
    thymeleaf时间戳转换
    alerttemplate 时间戳转换
    jQuery.Deferred exception: a.indexOf is not a function TypeError: a.indexOf is not a function
    区分数据是对象还是字符串
    eclipse中选取一列快捷键
    图片拉伸不变型
    这里ajax需要改成同步
    idea如何整理代码格式
    20170311-起早床
    20190310-解决头屑
  • 原文地址:https://www.cnblogs.com/CoreXin/p/7250143.html
Copyright © 2011-2022 走看看