MVC3客户端验证无法正常工作

时间:2011-01-26 09:45:09

标签: validation client asp.net-mvc-3

我的模型上有一个必需的注释:

[Required(ErrorMessage = "Please choose an option")]
public bool? AnyDebts { get; set; }

我已在web.config中启用了客户端验证:

   <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>

我在布局中引用了jquery脚本:

<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.4.4.js")" type="text/javascript"></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/jquery-ui-1.8.6.custom.min.js")" type="text/javascript"></script>

我还需要做些什么才能使客户端验证工作? 服务器端验证仍然有效。

编辑:

啊哈!

我发现客户端验证工作正常。

但是,具体来说,我发现未验证模型属性的客户端是使用自定义属性注释的那些。 例如:

[BooleanRequiredToBeTrue(ErrorMessage = "You must agree to the statements listed")]
public bool StatementAgree { get; set; }

属性的代码:

public class BooleanRequiredToBeTrueAttribute: RequiredAttribute
{
    public override bool IsValid(object value)
    {
        return value != null && (bool)value;
    }
}

这些未经验证的客户端是否已经过验证?

3 个答案:

答案 0 :(得分:6)

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

是客户端验证工作所需的唯一脚本。

一如既往这是一个完整的演示:

型号:

public class MyViewModel
{
    [Required(ErrorMessage = "Please choose an option")]
    public bool? AnyDebts { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}

查看:

@model AppName.Models.MyViewModel
@{
    ViewBag.Title = "Home Page";
}
<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>
@using (Html.BeginForm())
{
    <div>Debts</div>
    <span>Yes</span> @Html.RadioButtonFor(x => x.AnyDebts, true)
    <span>No</span> @Html.RadioButtonFor(x => x.AnyDebts, false)

    @Html.ValidationMessageFor(x => x.AnyDebts)
    <input type="submit" value="OK" />
}

备注:我没有在我的视图中添加jquery-1.4.4.js,因为它已在布局中引用。

答案 1 :(得分:1)

看看这段代码。

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
public class RequiredIfAttribute : RequiredAttribute
{
    public string PropertyName { get; set; }
    public Enums.BasicOperationType BasicOperationType { get; set; }

    public RequiredIfAttribute(string propertyName, Enums.BasicOperationType BasicOperationType)
    {
        this.PropertyName = propertyName;
        this.BasicOperationType = BasicOperationType;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        Type objectType = validationContext.ObjectType;
        PropertyInfo[] neededProperties = objectType.GetProperties().Where(propertyInfo => propertyInfo.Name == PropertyName).ToArray();
        if (Core.Helpers.EnumHelper.TryParse<Enums.BasicOperationType>(Convert.ToInt16(neededProperties[0].GetValue(validationContext.ObjectInstance, null))) == BasicOperationType)
            return base.IsValid(value, validationContext);

        return base.IsValid(0, validationContext);
    }
}

答案 2 :(得分:0)

对于我的情况,它只发生在怪癖模式下使用IE时,我的设置是浏览器模式:IE9,文档模式:怪癖。这导致客户端验证不适合我。但是,在使用开发人员工具栏时,同一页面在Firefox,Chrome和IE9,浏览器模式:IE9,文档模式:IE9标准中运行良好。这不是一个解决方案,而是一个可能的领导