本文介绍了MVC 3 jQuery 验证/数字/十进制字段的全球化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Web.config 文件中使用 globalculture="da-DK" 时,jQuery 验证不起作用.
在丹麦,当我们为 at 产品写价格时,我们使用符号 19,95 而不是美国方式 19.95,这给了我一个我无法解决的问题.
我已经启动了 VS2010,新的 MVC 3 项目,添加了一个 homeController、一个 Product 类和一个简单的标准编辑视图,但错误已经存在.
产品类别:
公共类产品{公共字符串名称 { 获取;放;}公共字符串 itemNo { get;放;}公开十进制价格{得到;放;}}
家庭控制器:
public class homeController : 控制器{公共 ActionResult 索引(){var product1 = new Product { name = "Testproduct", itemNo = "PRD-151541", price = 19 };返回视图(产品 1);}}
索引视图:
@model WebUI.DomainModel.Product<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)<字段集><legend>产品</legend><div class="editor-label">@Html.LabelFor(model => model.name)
<div class="editor-field">@Html.EditorFor(model => model.name)@Html.ValidationMessageFor(model => model.name)
<div class="editor-label">@Html.LabelFor(model => model.itemNo)
<div class="editor-field">@Html.EditorFor(model => model.itemNo)@Html.ValidationMessageFor(model =>model.itemNo)
<div class="editor-label">@Html.LabelFor(model =>model.price)
<div class="editor-field">@Html.EditorFor(model => model.price)@Html.ValidationMessageFor(model => model.price)
<p><input type="submit" value="保存"/></p></fieldset>}
结果:
很遗憾,我无法在此处提交图片 - 所以请点击此链接查看结果:http://www.designvision.dk/temp/mvc3_razor_validation_error.gif
SO - 运行网站时,该字段将设置为 19,00 - 这是正确的文化定义 - 但在尝试保存时,验证失败.
请帮忙...
解决方案
你可以试试 来自 Microsoft 的 jQuery 全球化插件:
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jquery.glob.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/globinfo/jquery.glob.da-dk.js")" type="text/javascript"></script><script type="text/javascript">$.validator.methods.number = 函数(值,元素){返回 !isNaN($.parseFloat(value));}$(函数(){$.preferCulture('da-DK');});
插件已重命名和移动,您应该使用 Globalize(2012 年 3 月)
<script src="@Url.Content("~/Scripts/jquery.globalize/globalize.js")" type="text/javascript"></script><script src="@Url.Content("~/Scripts/jquery.globalize/cultures/globalize.culture.da-DK.js")" type="text/javascript"></script><script type="text/javascript">$.validator.methods.number = 函数(值,元素){返回 !isNaN(Globalize.parseFloat(value));}$(document).ready(function () {Globalize.culture('da-DK');});
在 Scott Hanselman 博文
上了解更多相关信息When using globalization culture="da-DK" in the Web.config file, the jQuery validation does not work.
In Denmark, we use the notation 19,95 instead of the US way 19.95 when we write a price for at product, and that have given me a problem, that I can't solve.
I've started VS2010, new MVC 3 project, added a homeController, a Product class, and a simple standard edit view and the error is already there.
Product Class:
public class Product
{
public string name { get; set; }
public string itemNo { get; set; }
public decimal price { get; set; }
}
HomeController:
public class homeController : Controller
{
public ActionResult Index()
{
var product1 = new Product { name = "Testproduct", itemNo = "PRD-151541", price = 19 };
return View(product1);
}
}
Index View:
@model WebUI.DomainModel.Product
<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>Product</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.itemNo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.itemNo)
@Html.ValidationMessageFor(model => model.itemNo)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.price)
@Html.ValidationMessageFor(model => model.price)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
The result:
Unfortunately I can't submit an image here - so please follow this link to see the result:http://www.designvision.dk/temp/mvc3_razor_validation_error.gif
SO - when running the website, the field will be set to 19,00 - which IS the correct culture definition - but when trying to save, validation fails.
Please help...
解决方案
You could try the jQuery Globalization plugin from Microsoft:
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.glob.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/globinfo/jquery.glob.da-dk.js")" type="text/javascript"></script>
<script type="text/javascript">
$.validator.methods.number = function (value, element) {
return !isNaN($.parseFloat(value));
}
$(function () {
$.preferCulture('da-DK');
});
</script>
Plugin was renamed and moved, you should use Globalize (Mar 2012)
<script src="@Url.Content("~/Scripts/jquery.globalize/globalize.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.globalize/cultures/globalize.culture.da-DK.js")" type="text/javascript"></script>
<script type="text/javascript">
$.validator.methods.number = function (value, element) {
return !isNaN(Globalize.parseFloat(value));
}
$(document).ready(function () {
Globalize.culture('da-DK');
});
</script>
more about this on Scott Hanselman blog post
这篇关于MVC 3 jQuery 验证/数字/十进制字段的全球化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-04 03:29