带有附加 bool 字段的 MVC 远程验证 [英] MVC Remote Validation With Additional bool Fields

查看:24
本文介绍了带有附加 bool 字段的 MVC 远程验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有附加 bool 复选框字段的远程验证

I am trying to use Remote Validation with an additional bool checkbox field

[Remote("IsStorageConnectionValid", "TenantManagement", AdditionalFields = "CreateStorage")]
public String StorageConnectionString { get; set; }

验证码

public JsonResult IsStorageConnectionValid(string storageConnectionString, bool createStorage){

它在击中验证器方面工作得很好.但是,无论复选框的值如何, createStorage 始终为真.如果我使用不是复选框的其他字段,它们会被完美地提供.

It works perfectly in terms of it hitting the validator. However createStorage is always true irrespective of the value of the checkbox. If I use additional fields that aren't check boxes they are supplied perfectly.

按标准创建的复选框:

  @Html.CheckBoxFor(m => m.CreateStorage)

这是一个错误吗?还是我做错了?

Is this a bug? Or am I doing it wrong?

Fiddle 来了(我认为是 MVC4 但做同样的事情)

推荐答案

当与 @Html.CheckBoxFor 一起使用时,这似乎是一个错误.问题是 CheckBoxFor 呈现 2 个元素,一个带有 value="true" 的复选框和一个带有 value="false" 的隐藏输入(未选中复选框不会回传,因此第二个隐藏输入确保一个值被回传以供 DefaultModelBinder)

It does appear that this is a bug when used with @Html.CheckBoxFor. The problem is that CheckBoxFor renders 2 elements, a checkbox with value="true" and a hidden input with value="false" (Unchecked checkboxes do not post back so the second hidden input ensures a value is posted back for use by the DefaultModelBinder)

查看jquery.validate.unobtrusive.js文件的相关部分

adapters.add("remote", ["url", "type", "additionalfields"], function (options) {
  var value = {
    url: options.params.url,
    type: options.params.type || "GET",
    data: {}
  },
  prefix = getModelPrefix(options.element.name);

  $.each(splitAndTrim(options.params.additionalfields || options.element.name), function (i, fieldName) {
    var paramName = appendModelPrefix(fieldName, prefix);
    value.data[paramName] = function () {
      return $(options.form).find(":input[name='" + escapeAttributeValue(paramName) + "']").val();
    };
  });

  setValidationValues(options, "remote", value);
});

return 语句返回(在您的情况下).find(':input[name="CreateStorage"]').val(); 返回值第一个输入的name="CreateStorage"总是true(复选框的值)

the return statement returns (in your case) .find(':input[name="CreateStorage"]').val(); which returns the value of the first input with the name="CreateStorage" which will always be true (the value of the checkbox)

作为测试,如果您使用 HiddenFor 而不是 CheckBoxFor 呈现值,您将在 IsStorageConnectionValid 方法中收到正确的值(但当然这确实有帮助,因为您无法更改该值)

As a test, if you render the value using HiddenFor rather that CheckBoxFor you will receive the correct value in your IsStorageConnectionValid method (but of course this does help since you cant change the value)

不确定最好的解决方案,但 unobtrusive 脚本应该首先检查 .find(..) 是否返回多个元素,然后如果第一个是未选中的复选框,返回第二个元素的值.

Not sure of the best solution, but the unobtrusive script should be first checking if .find(..) returns more than one element, then if the first is a checkbox which is unchecked, returning the value of the second element.

编辑

我已将此报告为 Codeplex 的问题

编辑 2

我被告知该问题现已在此处修复

这篇关于带有附加 bool 字段的 MVC 远程验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆