我試圖讓我的客戶端驗證(模型綁定)來支持不同的文化,並且我發現了一個有趣的博客,我正在努力實施。MVC 3客戶端驗證,模型綁定十進制值和文化(不同的小數點分隔符)
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中被改變使用我的自定義模型綁定器在小數和十進制?值
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驗證不處理逗號。
擴展驗證後,是否還需要DecimalModelBinder? – Romias 2012-04-03 12:19:48
是的,你這樣做。如果不是那麼ModelBinder(服務器端)將添加一個錯誤。實際上,如果您不想在POST之前進行驗證,則只能使用DecimalModelBinder。 – Kman 2012-04-03 12:25:02
很高興你解決了它,並感謝張貼解決方案 – 2012-04-03 14:02:24