使用腳本提交集合类时,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());