Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

asp.net mvc - MVC Remote Validation With Additional bool Fields

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...