相关疑难解决方法(0)

无法让jquery全球化发挥作用

我尝试使用jQuery Globalization插件来修复jquery无阻碍客户端验证的逗号问题.但是我尝试了很多解决方案,没有很好的解决方案来解决这个问题.我在非英语本地化计算机上,这对我的客户输入十进制值如"123,66"而不是"123.66"很重要.ASP.NET验证告诉我,价格必须是一个数字!嗯?你是认真的吗 ?大声笑

当我尝试修复时,我收到此javascript错误.

$.global is undefined
Run Code Online (Sandbox Code Playgroud)

在这里我的代码.

Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

    <script src="@Url.Content("~/Scripts/jquery-1.7.1.js")" type="text/javascript"></script>

    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")"></script>
    <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/globalize.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/cultures/globalize.cultures.js")" type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/jquery.form.js")"type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/glob.fix.js")" type="text/javascript"></script>
</head>

<body>
    @RenderBody()
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

glob.fix.js

$.validator.methods.range = function (value, element, param) {
   var globalizedValue = value.replace(",", ".");
   return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
}

$.validator.methods.number = function (value, element) {
   return this.optional(element) || …
Run Code Online (Sandbox Code Playgroud)

jquery-globalization

18
推荐指数
1
解决办法
3万
查看次数

使用ComponentModel.DataAnnotations进行MVC3验证的英国日期格式(也使用jquery ui datepicker)

我看到有一些类似的问题,但没有一个能解决我的问题.

我正在使用Entity Framework 4.3开发MVC3应用程序.我有一个英国日期字段,我计划允许用户使用Jquery UI datepicker进行编辑(由于这个博客我得到了工作).

幸运的是,这个博客包含了使用英国格式制作日期选择器的说明,但是,EF验证仍然告诉我需要输入有效的日期格式.很奇怪,它并没有阻止我将日期提交给数据库,只是不显眼的验证开始并显示消息.

目前我有以下数据注释:

[DataType(DataType.Date)]
public System.DateTime Module_Date { get; set; }
Run Code Online (Sandbox Code Playgroud)

但我也试过添加:

[DisplayFormat(DataFormatString="{0:dd/MM/yyyy}")]
Run Code Online (Sandbox Code Playgroud)

这完全没有效果.我希望有人有一个解决方案,因为我不喜欢关闭不引人注意的验证来停止此错误消息.

谢谢

编辑

在@Iridio回答之后,我考虑添加一个模型绑定,实际上从像我这样读过它的几个帖子似乎是正确的做法,但我想出的并没有效果.这是我尝试过的:

public class DateTimeBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        var date = value.ConvertTo(typeof(DateTime), CultureInfo.CurrentCulture);

        return date;
    }
}
Run Code Online (Sandbox Code Playgroud)

Application_Start()Global.asax.cs文件的方法中使用此方法:

ModelBinders.Binders.Add(typeof(DateTime), new DateTimeBinder());
ModelBinders.Binders.Add(typeof(DateTime?), new DateTimeBinder());
Run Code Online (Sandbox Code Playgroud)

c# validation entity-framework data-annotations asp.net-mvc-3

6
推荐指数
1
解决办法
3723
查看次数