C# MVC4远程验证未接收参数值
我正在尝试为视图中的字段实现远程验证。到目前为止,除了validation controller方法中的参数为null之外,所有内容都正常工作,即使该字段包含值。我错过了什么 验证控制器方法C# MVC4远程验证未接收参数值,c#,asp.net-mvc,validation,asp.net-mvc-4,jquery-validation-engine,C#,Asp.net Mvc,Validation,Asp.net Mvc 4,Jquery Validation Engine,我正在尝试为视图中的字段实现远程验证。到目前为止,除了validation controller方法中的参数为null之外,所有内容都正常工作,即使该字段包含值。我错过了什么 验证控制器方法 public JsonResult IsVanityURL_Available(string VanityURL) { if (!_webSiteInfoRepository.GetVanityURL(VanityURL)) return Json(true, JsonRequest
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);
}
看法
@Html.LabelFor(model=>model.SelectedContact.WebSiteInfoes[0].VanityURL)
@Html.TextBoxFor(model=>model.SelectedContact.WebSiteInfoes[0].VanityURL,新的{@class=“form control”,@placeholder=“Enter-URL”})
更新
重复帖子中的答案确实解决了问题。更改您的实体
change your entity
[DisplayName("Vanity URL")]
[Remote("IsVanityURL_Available", "Validation",AdditionalFields = "VanityURL")]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")]
[Editable(true)]
public string VanityURL { get; set; }
and add this to your view
@{
var VanityURL=Model.SelectedContact.WebSiteInfoes[0].VanityURL
}
<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.TextBox("VanityURL",VanityURL,new { @class = "form-control", @placeholder = "Enter Vanity URL" })
</div>
</div>
</div>
[显示名称(“虚荣URL”)]
[远程(“IsVanityURL_可用”、“验证”,AdditionalFields=“VanityURL”)]
[RegularExpression(@“(\S)+”,ErrorMessage=“不允许空白。”)]
[可编辑(正确)]
公共字符串VanityURL{get;set;}
并将其添加到您的视图中
@{
var VanityURL=Model.SelectedContact.WebSiteInfoes[0]。VanityURL
}
@Html.LabelFor(model=>model.SelectedContact.WebSiteInfoes[0].VanityURL)
@TextBox(“VanityURL”,VanityURL,new{@class=“form control”,@placeholder=“Enter vanitary URL”})
我找到了另一种避免更改jquery.validate.js文件的方法。这涉及到在视图中设置TextBoxFor的名称,例如
@Html.TextBoxFor(model => model.SelectedContact.WebSiteInfoes[0].VanityURL, new { @class = "form-control", @placeholder = "Enter Vanity URL", @Name="VanityUrl })
我恢复了对js文件的更改,然后添加了视图名称属性和模型远程AdditionalFields定义的组合,效果很好
这一变化也造成了一些无法预料的问题。我终于找到了解决办法。我更改了GET to a POST并从FormsCollection中获取了所需的值。这让我走上了正确的方向。这使我能够完全绕过复杂的数据对象命名问题可能重复的感谢指针。我用建议的更改更改了jquery.validate文件,但它没有更改任何内容。参数仍然为空。请使用浏览器工具在js文件上放置断点,并检查
数据的值是否为{VanityURL:someValue}
抱歉,它确实解决了问题。我的BundleConfig使用的是我没有更改的min版本。谢谢你的帮助。谢谢,但我已经试过了,没有成功。该值仍然为空。请发布您的整个entitylets查看此更新的答案是否适合您
change your entity
[DisplayName("Vanity URL")]
[Remote("IsVanityURL_Available", "Validation",AdditionalFields = "VanityURL")]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")]
[Editable(true)]
public string VanityURL { get; set; }
and add this to your view
@{
var VanityURL=Model.SelectedContact.WebSiteInfoes[0].VanityURL
}
<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.TextBox("VanityURL",VanityURL,new { @class = "form-control", @placeholder = "Enter Vanity URL" })
</div>
</div>
</div>
@Html.TextBoxFor(model => model.SelectedContact.WebSiteInfoes[0].VanityURL, new { @class = "form-control", @placeholder = "Enter Vanity URL", @Name="VanityUrl })