ASP.NET/MVC:基因敲除绑定阻止了客户端验证的执行?

ASP.NET/MVC: Knockout-binding preventing client-side validation from being performed?

提问人:Holland 提问时间:6/9/2017 最后编辑:Hakan FıstıkHolland 更新时间:6/10/2017 访问量:111

问:

这是此问题的后续,或者更确切地说是替代: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 会以某种方式阻止客户端验证的执行?

客户端 knockout.js ASP.NET-MVC 验证

评论

1赞 Bryan Dellinger 6/9/2017
如果将绑定更改为可见绑定而不是 if 绑定,它是否有效
0赞 Holland 6/9/2017
@BryanDellinger 谢谢,但不,结果一样。(这确实让我意识到我应该查找何时使用“if”以及何时使用“visible”。
1赞 Jose Luis 6/9/2017
我看到一个带有html的问题,我更新了小提琴:jsfiddle.net/xh4c2pbr/4。但它有效。我无法重现您的问题。
1赞 Holland 6/9/2017
谢谢何塞,我现在必须走了,明天早上会回来看看(现在欧洲已经下午结束了)。
1赞 Bryan Dellinger 6/9/2017
@Holland,您可能知道,但有一个专门为 asp.net MVC 制作的 knockout 库。knockoutmvc.com

答: 暂无答案