我正在尝试让我的客户端验证(模型绑定(bind))来支持不同的文化,并且我发现了一个关于我正在尝试实现的主题的有趣博客。
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, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, 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?值(value)观
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);
}
客户端验证仍然失败,在我的 View 中输入的十进制以“,”作为小数分隔符。它不能同时处理“,”和“.”。 js 验证似乎没有考虑我的自定义绑定(bind)
一遍又一遍地阅读博客文章,我似乎无法弄清楚我错过了什么。
这是 View :
@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 验证不处理逗号。
最佳答案
最后,通过了解自定义 DecimalModelBinder 只会处理服务器端验证而不影响处理客户端验证的 jquery.validate.js,我找到了解决问题的方法。
扩展验证解决了我的问题。
通过新的 .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
关于asp.net-mvc-3 - MVC 3客户端验证,模型绑定(bind)十进制值和文化(不同的小数分隔符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9980104/