我在表单中使用RemoteAttribute
进行自定义验证。为简单起见,我有一个广播组PrimarySupportingDocument
,有3个选项。如果是特定选择,则远程验证应验证两个文本框值VIN
和TitleNumber
是否基于某些(不相关)标准进行匹配。
在我的 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);
}
表单示例:
因此,如果查看请求网址,您可以看到它将In-State+Title
作为PrimarySupportingDocument
值而不是实际:checked
值Out 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>
答案 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
元素而不是单选按钮