C# RemoteAttribute验证传递的附加字段值错误
我在表单中使用C# RemoteAttribute验证传递的附加字段值错误,c#,jquery,jquery-validate,asp.net-mvc-5,C#,Jquery,Jquery Validate,Asp.net Mvc 5,我在表单中使用RemoteAttribute进行一些自定义验证。为了简单起见,我有一个广播组,PrimarySupportingDocument,有3个选项。如果是特定选择,远程验证应验证两个文本框值,VIN和TitleNumber,是否基于某些(无关)标准匹配 在my视图模型中: [Required] [DisplayName("VIN")] public string VIN { get; set; } [Required] [DisplayName
RemoteAttribute
进行一些自定义验证。为了简单起见,我有一个广播组,PrimarySupportingDocument
,有3个选项。如果是特定选择,远程验证应验证两个文本框值,VIN
和TitleNumber
,是否基于某些(无关)标准匹配
在my视图模型中:
[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; }
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);
}
在myValidationController中:
[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; }
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,可以看到它将状态+标题
传递为主要支持文档
值,而不是实际的:选中的值状态外标题
我做了一些研究,看看从jquery.validate
传递的实际值是否正确并被找到。这不是问题所在,因为它已在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
不支持单选按钮!它不按选中的属性进行过滤
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
};
});
...
});
最简单的解决方法是使用选择
元素而不是单选按钮我从这个答案中找到了解决方案:这个问题应该标记为重复的问题,但我保持开放状态,希望它能帮助其他人。+1感谢您的帮助,它引导我找到了一个解决方法。见我上面的评论