提问人:Holland 提问时间:6/9/2017 最后编辑:Hakan FıstıkHolland 更新时间:6/10/2017 访问量:111
ASP.NET/MVC:基因敲除绑定阻止了客户端验证的执行?
ASP.NET/MVC: Knockout-binding preventing client-side validation from being performed?
问:
这是此问题的后续,或者更确切地说是替代:MVC/ASP.NET:客户端验证不起作用
问题过去和现在都是,我的 ASP.NET/MVC 客户端验证没有被执行,即使它应该被执行。
现在我已经做了更多的测试,似乎我已经确定了问题的核心,所以就这样吧。
我有一个 ViewModel,如下所示:
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
// some other properties...
}
以及我的视图中的相应表单(同样,仅显示电子邮件字段的代码):
@model Treinen2.Models.RegisterViewModel
@{
ViewBag.Title = "Register";
}
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Create a new account.</h4>
<hr />
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Email)
</div>
</div>
}
这很好用,完全没有问题。
但是,现在我将这个完全相同的形式放在包含 Knockout-binding 的 DIV 元素中,如下所示:
<div data-bind="if: model.getoondePagina().register">
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new {
@class = "form-horizontal", role = "form" }))
{
<!-- Here goes exact same form as above -->
}
</div>
不幸的是,在这个 DIV 中,客户端验证不再起作用。
也就是说,正确的 HTML(具有正确的验证属性)仍在生成,但实际的错误消息(例如键入无效的电子邮件地址或将字段留空后)不再出现。我还注意到,Knoutout-binding本身就完美地工作。MVC 客户端验证也是如此,直到我将其放入 Knockout-bound DIV 中。因此,显然不存在其他问题,似乎敲除绑定是唯一的罪魁祸首,或者更确切地说是敲除和客户端验证之间的交互。
所以我认为我的问题是:为什么 Knockout-binding 会以某种方式阻止客户端验证的执行?
答: 暂无答案
上一个:数据验证实施
评论