我发现在向TextBoxFor添加@Name属性后,我的ValidationMessageFor停止了工作。这样做,但我如何让ValidationMessageFor使用自定义名称属性。我需要维护自定义名称属性。
<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", @Name="VanityUrl" })
</div>
@Html.ValidationMessageFor(model => model.SelectedContact.WebSiteInfoes[0].VanityURL)
参见相关帖子here
所以我做了一些更改并回到修改后的jquery.validate.js,问题是第二个值永远不会进入Validation方法。
模型字段
[DisplayName("Vanity URL")]
[Remote("IsVanityURL_Available", "Validation", AdditionalFields = "ContactId")]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")]
[Editable(true)]
public string VanityURL { get; set; }
请求的提琴手陷阱
方法中的参数
答案 0 :(得分:1)
您必须使用相应HTML控制器data-valmsg-for
的值更新 ValidationMessageFor 帮助程序的name
属性。
@Html.ValidationMessageFor(model => model.SelectedContact.WebSiteInfoes[0].VanityURL,
null, new { data_valmsg_for = "VanityUrl" })
答案 1 :(得分:0)
您可以将其附加到@class
:
@Html.TextBoxFor(
model => model.SelectedContact.WebSiteInfoes[0].VanityURL,
new { @class = "form-control VanityURL", placeholder = "Enter Vanity URL" })
var vanityUrl = $('.VanityURL').first();
答案 2 :(得分:0)