c# - RemoteAttribute 验证传递了附加字段的错误值

标签 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

因此,如果您查看请求 URL,您会发现它正在将 In-State+Title 作为 PrimarySupportingDocument 值而不是实际的 传递:检查州外标题

我做了一些研究,看看从 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>

最佳答案

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

关于c# - RemoteAttribute 验证传递了附加字段的错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20482095/

相关文章:

c# - 在 ASP.Net MVC 应用程序中创建 Outlook 模板 (.oft) 文件

c# - 使用 out 参数在 NSubstitute 模拟方法中返回不同的值

C# 窗体,属性不起作用?

javascript - 多个 jQuery 工具提示相互干扰

jQuery 验证插件添加多个 .rules() 函数

c# - 正则表达式问题匹配前后搜索词

javascript - JQGrid 表格上的自定义标题行

javascript - 轮播 ImageView - 内联 block

jquery - 为什么 JQuery Form SubmitHandler 不刷新其回调?

javascript - 在提交之前验证表单