MVC远程验证随着附加布尔字段 [英] MVC Remote Validation With Additional bool Fields

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

问题描述

我尝试使用远程验证一个额外的布尔复选框字段

  [远程(IsStorageConnectionValid,TenantManagement,AdditionalFields =CreateStorage)]
公共字符串StorageConnectionString {搞定;组; }

验证code

 公共JsonResult IsStorageConnectionValid(字符串storageConnectionString,布尔createStorage){

它完美地击中了验证它的条款。然而createStorage总是复选框的值的真不管。如果我使用附加字段未复选框它们是完全提供。

复选框作为标准发布:

  @ Html.CheckBoxFor(M = GT; m.CreateStorage)

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

小提琴是在这里(是MVC4我想,但做同样的事情)


解决方案

但看来这是一个bug与 @ Html.CheckBoxFor 使用时。问题是, CheckBoxFor 渲染2个元素,用价值=真实和一个隐藏的输入与<$复选框C $ C>值=FALSE(未选中复选框不回发所以第二个隐藏的输入保证值发布回通过使用 DefaultModelBinder

综观有关节的 jquery.validate.unobtrusive.js 文件

  adapters.add(远程,[网址,型,additionalfields],功能(选件){
  VAR值= {
    网址:options.params.url,
    类型:options.params.type || 得到,
    数据:{}
  },
  preFIX = getModel preFIX(options.element.name);  $。每个(splitAndTrim(options.params.additionalfields || options.element.name),功能(I,字段名){
    VAR paramName配置= appendModel preFIX(字段名,preFIX);
    value.data [paramName配置] =功能(){
      返回$(options.form).find(:输入[名称='+ escapeAttributeValue(paramName配置)+'])VAL();
    };
  });  setValidationValues​​(选项,远程,值);
});

收益语句返回(你的情况) .find(':输入[名称=CreateStorage])VAL() ; 它返回的第一输入与的值名称=CreateStorage这将永远是真正(复选框的值)

作为测试,如果使用渲染值 HiddenFor 宁可 CheckBoxFor 您将收到正确的值你的 IsStorageConnectionValid 方法(当然这确实有助于因为你不能改变的值)

不知道最好的解决办法,但不显眼脚本应该能 .find先检查(..)返回多个元素,那么如果第一个是一个复选框是选中,返回第二个元素的值。

修改

我报告这是在codePLEX 问题>

编辑2

我已被告知现在的问题一直<一个href=\"https://aspnetwebstack.$c$cplex.com/SourceControl/changeset/8aee6c29e69a32234663377b8cbb0e8fda5da76e\"相对=nofollow>这里固定

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

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

Validation code

public JsonResult IsStorageConnectionValid(string storageConnectionString, bool 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.

Checkbox created as standard:

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

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

Fiddle Is here (Is MVC4 I think but does the same thing)

解决方案

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)

Looking at the relevant section of the jquery.validate.unobtrusive.js file

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);
});

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)

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)

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.

Edit

I have reported this as an issue at Codeplex

Edit 2

I have been advised the the issue has now been fixed here

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

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