【问题标题】:RemoteAttribute Validation passes the wrong value of AdditionalFieldsRemoteAttribute Validation 传递了 AdditionalFields 的错误值
【发布时间】:2013-12-09 22:21:18
【问题描述】:

我正在使用RemoteAttribute 在我的表单中进行一些自定义验证。为简单起见,我有一个广播组PrimarySupportingDocument,有 3 个选项。如果是某个选择,则远程验证应验证两个文本框值 VINTitleNumber 基于某些(不相关)标准匹配。

在我的ViewModel中:

    [Required]
    [DisplayName("VIN")]
    public string VIN { get; set; }

    [Required]
    [DisplayName("Primary Supporting Document")]
    public string PrimarySupportingDocument { get; set; }

    [DisplayName("Title Number")]
    [Remote("VinAndTitleMatch", "Validation", ErrorMessage = "VIN and Title Number do not match", AdditionalFields = "VIN, PrimarySupportingDocument")]
    public string TitleNumber { get; set; }

在我的 ValidationController 中:

    public JsonResult VinAndTitleMatch(string titleNumber, string VIN, string primarySupportingDocument)
    {
        if (primarySupportingDocument == "In-State Title" && VIN != null && titleNumber != null)
        {


            if (/* test if vin and title number match */)
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }

            return Json("VIN and Title Number do not match", JsonRequestBehavior.AllowGet);
        }

        return Json(true, JsonRequestBehavior.AllowGet);
    }

表单示例:

因此,如果您查看请求 URL,您可以看到它将 In-State+Title 作为 PrimarySupportingDocument 值而不是实际的 :checkedOut of State Title 传递。

我做了一些研究,看看从jquery.validate 传递的实际值是否正确并找到this issue。这不是问题,因为它已在 1.11.1 中修复,并且我记录了该行的返回值的输出: $("input[name='" + $(element).attr("name") + "']:checked").val() 返回正确的值,但我似乎无法弄清楚为什么它没有通过无线电组的检查值。

这是广播组的渲染输出:

<div class="form-group">
    <label class="control-label" for="PrimarySupportingDocument">Primary Supporting Document</label>
    <div class="radio">
        <label>
            <input data-val="true" data-val-required="The Primary Supporting Document field is required." id="PrimarySupportingDocument0" name="PrimarySupportingDocument" type="radio" value="In-State Title">In-State Title</label>
    </div>
    <div class="radio">
        <label>
            <input id="PrimarySupportingDocument1" name="PrimarySupportingDocument" type="radio" value="Out of State Title">Out of State Title</label>
    </div>
    <div class="radio">
        <label>
            <input id="PrimarySupportingDocument2" name="PrimarySupportingDocument" type="radio" value="None">None</label>
    </div> 
    <span class="field-validation-valid" data-valmsg-for="PrimarySupportingDocument" data-valmsg-replace="true"></span>
</div>

【问题讨论】:

  • 我能够从这个答案中找到解决方案:stackoverflow.com/a/19850382/998320 这应该被标记为一个重复的问题,但我保持开放,希望它可以帮助其他人。

标签: c# jquery jquery-validate asp.net-mvc-5


【解决方案1】:

很遗憾,jquery.validate.unobtrusive.js 不支持单选按钮!它不按checked 属性过滤

adapters.add("remote", ["url", "type", "additionalfields"], function (options) {
    ...
    $.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").filter("[name='" + escapeAttributeValue(paramName) + "']").val(); //problem here
        };
    });
    ...
});

解决此问题的最简单方法是使用 select 元素而不是单选按钮

【讨论】:

  • +1 感谢您的帮助,它引导我找到解决方法。看我上面的评论
猜你喜欢
  • 1970-01-01
  • 2022-11-14
  • 1970-01-01
  • 2022-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多