我正在尝试验证“Double”类型的用户输入,但我有非常奇怪的行为
模型
public class User
{
public double CodeA { get; set; }
public double CodeB { get; set; }
}
查看
<div class="row">
<div class="form-group">
<div class="col-md-1">
<label class="control-label">CodeA</label>
</div>
<div class="col-md-2">
@Html.TextBoxFor(model => model.CodeA, new { @class = "form-control input-sm cus-read" })
</div>
<div class="col-md-3">
@Html.ValidationMessageFor(model => model.CodeA)
</div>
<div class="col-md-1">
<label class="control-label">CodeB</label>
</div>
<div class="col-md-2">
@Html.TextBoxFor(model => model.CodeB, new { @class = "form-control input-sm cus-read" })
</div>
<div class="col-md-3">
@Html.ValidationMessageFor(model => model.CodeB)
</div>
</div>
</div>
输入为CodeA =“1,09”,CodeB =“2,11” 提交表单时,我收到CodeA“输入必须是数字”的错误消息 但CodeB没有错误
到目前为止,我确实添加了以下内容:在web.config中
<system.web>
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="fr-BE"
uiCulture="fr-BE"
/>
</system.web>
我还在这里读到,我还必须包括客户端验证: Found Here
我应该在哪里添加此文件? 我在JqueryValidate调用之前和之后添加但是什么都没发生? 我应该复制代码并添加到 jquery.validate.js文件吗? 我想添加这个文件: https://github.com/jquery-validation/jquery-validation/blob/master/src/localization/methods_nl.js
我也使用了ModelBinder类但没有成功:
public override 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.ToDouble(valueResult.AttemptedValue,
CultureInfo.CurrentCulture);
}
catch (FormatException e)
{
modelState.Errors.Add(e);
}
bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
return actualValue;
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(function() { // will trigger when the document is ready
$('.datepicker').datepicker({
format: "dd/mm/yy",
todayBtn: "linked",
autoclose: true,
todayHighlight: true
}); //Initialise any date pickers
});
$.extend($.validator.methods, {
date: function (value, element) {
return this.optional(element) || /^\d\d?[\.\/\-]\d\d?[\.\/\-]\d\d\d?\d?$/.test(value);
}
});
</script>
jqueryval是验证bunles