【发布时间】:2015-03-24 02:28:28
【问题描述】:
我正在尝试对视图中的字段实施远程验证。到目前为止一切正常,除了验证控制器方法中的参数为空,即使该字段包含一个值。我错过了什么?
验证控制器方法
public JsonResult IsVanityURL_Available(string VanityURL)
{
if (!_webSiteInfoRepository.GetVanityURL(VanityURL))
return Json(true, JsonRequestBehavior.AllowGet);
string suggestedUID = String.Format(CultureInfo.InvariantCulture,
"{0} is not available.", VanityURL);
for (int i = 1; i < 100; i++)
{
string altCandidate = VanityURL + i.ToString();
if (_webSiteInfoRepository.GetVanityURL(altCandidate)) continue;
suggestedUID = String.Format(CultureInfo.InvariantCulture,
"{0} is not available. Try {1}.", VanityURL, altCandidate);
break;
}
return Json(suggestedUID, JsonRequestBehavior.AllowGet);
}
实体属性
[DisplayName("Vanity URL")]
[Remote("IsVanityURL_Available", "Validation")]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")]
[Editable(true)]
public string VanityURL { get; set; }
查看
<div class="row">
<div class="form-group col-md-12">
<div class="editor-label">
@Html.LabelFor(model => model.SelectedContact.WebSiteInfoes[0].VanityURL)
</div>
<div class="input-group margin-bottom-small">
<span class="input-group-addon"><i class="fa fa-external-link-square fa-fw"></i></span>
@Html.TextBoxFor(model => model.SelectedContact.WebSiteInfoes[0].VanityURL, new { @class = "form-control", @placeholder = "Enter Vanity URL" })
</div>
</div>
</div>
更新 重复帖子中的答案确实解决了问题。
【问题讨论】:
-
感谢斯蒂芬的指点。我使用建议的更改更改了 jquery.validate 文件,但它没有更改任何内容。该参数仍为空。
-
用你的浏览器工具在js文件上下断点,检查
data的值是否真的是{ VanityURL: someValue } -
抱歉,它确实解决了问题。我的 BundleConfig 使用的是我没有更改的最低版本。谢谢你的帮助。
标签: c# asp.net-mvc validation asp.net-mvc-4 jquery-validation-engine