我试图让我的客户端验证(模型绑定)来支持不同的文化,我发现一个有趣的博客,我正在尝试实现这个主题.
http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx
波科
- public class Jogador
- {
- public int ID { get; set; }
- public string Name { get; set; }
- public decimal Salary { get; set; }
- }
我有我的自定义DecimalModelBinder类
- public class DecimalModelBinder : IModelBinder
- {
- public object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)
- {
- ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
- ModelState modelState = new ModelState {Value = valueResult};
- object actualValue = null;
- try
- {
- actualValue = Convert.ToDecimal(valueResult.AttemptedValue,CultureInfo.CurrentCulture);
- }
- catch (FormatException e)
- {
- modelState.Errors.Add(e);
- }
- bindingContext.ModelState.Add(bindingContext.ModelName,modelState);
- return actualValue;
- }
- }
我的web.config:
- <compilation debug="true" targetFramework="4.0">
- <assemblies>
- <add assembly="System.Web.Abstractions,Version=4.0.0.0,Culture=neutral,PublicKeyToken=31BF3856AD364E35" />
- <add assembly="System.Web.Helpers,Version=1.0.0.0,PublicKeyToken=31BF3856AD364E35" />
- <add assembly="System.Web.Routing,PublicKeyToken=31BF3856AD364E35" />
- <add assembly="System.Web.Mvc,Version=3.0.0.0,PublicKeyToken=31BF3856AD364E35" />
- <add assembly="System.Web.WebPages,PublicKeyToken=31BF3856AD364E35" />
- </assemblies>
- </compilation>
- <authentication mode="Forms">
- <forms loginUrl="~/Account/logon" timeout="2880" />
- </authentication>
- <pages>
- <namespaces>
- <add namespace="System.Web.Helpers" />
- <add namespace="System.Web.Mvc" />
- <add namespace="System.Web.Mvc.Ajax" />
- <add namespace="System.Web.Mvc.Html" />
- <add namespace="System.Web.Routing" />
- <add namespace="System.Web.WebPages"/>
- </namespaces>
- </pages>
Global.asax被改变为使用我的自定义ModelBinder十进制和十进制?值
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
- ModelBinders.Binders.Add(typeof(decimal),new DecimalModelBinder());
- ModelBinders.Binders.Add(typeof(decimal?),new DecimalModelBinder());
- RegisterGlobalFilters(GlobalFilters.Filters);
- RegisterRoutes(RouteTable.Routes);
- }
仍然在我的视图中,使用“,”作为小数分隔符输入的十进制的客户端验证失败.它不处理“,”和“.”. js验证似乎没有考虑到我的自定义绑定
这是视图:
- @model MVC_Empty.Web.Models.Jogador
- @{
- ViewBag.Title = "Create";
- }
- <h2>Create</h2>
- <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
- <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
- @using (Html.BeginForm()) {
- @Html.ValidationSummary(true)
- <fieldset>
- <legend>Jogador</legend>
- <div class="editor-label">
- @Html.LabelFor(model => model.Name)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Name)
- @Html.ValidationMessageFor(model => model.Name)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Salary)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Salary)
- @Html.ValidationMessageFor(model => model.Salary)
- </div>
- <p>
- <input type="submit" value="Create" />
- </p>
- </fieldset>
- }
- <div>
- @Html.ActionLink("Back to List","Index")
- </div>
服务器端验证似乎很好,但是如何处理客户端验证,以便在点击提交按钮时发送POST.
JavaScript验证不处理逗号.
解决方法
最后,通过了解Custom DecimalModelBinder只处理服务器端验证,并且不影响处理客户端验证的jquery.validate.js,我找到了一个解决问题的方法.
扩展验证解决了我的问题.
- $.validator.methods.number = function(value,element) {
- return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/ .test(value);
- };
这个博客真的很有帮助
http://rebuildall.umbraworks.net/2011/03/02/jQuery_validate_and_the_comma_decimal_separator