RemoteAttribute Validation传递的AdditionalFields值错误

时间:2013-12-09 22:21:19

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

我在表单中使用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);
    }

表单示例: example of problem

因此,如果查看请求网址,您可以看到它将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>

1 个答案:

答案 0 :(得分:2)

不幸的是,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元素而不是单选按钮