我如何编程触发客户端验证通过的Razor视图设置? [英] How can I programmatically trigger client side validation set up by a Razor view?

查看:143
本文介绍了我如何编程触发客户端验证通过的Razor视图设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初产生的,我用剃刀观点有点儿Ajax应用程序的HTML表单片段,我后来阅读并从knockout.js写。虽然我没有做非Ajax请求的动作,我用剃刀来生成HTML,所以我享受自动生成的jQuery验证属性。例如。在我的单页,我渲染一个隐藏的表单是这样的:

I have a little Ajax application where I use Razor views to initially generated HTML form segments that I later read and write from with knockout.js. Although I am doing no non-Ajax action requests, I use Razor to generate the HTML so I enjoy automatic generation of jQuery Validation attributes. E.g. in my single page, I render a hidden form like this:

<section id="person-detail">
    @Html.Action("EditPartial", "Person")
</section>

EditPartial 操作返回一个看起来很像这样的局部视图:

The EditPartial action returns a partial view that looks a lot like this:

@using (Html.BeginForm())
{
    <fieldset>
        @Html.HiddenFor(model => model.Id, new { data_bind = "value: id" })
        <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.FirstName, new { data_bind = "value: firstName" })
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>
        <p>            
            <a href="#" data-bind="click: save">Update</a>&nbsp;&nbsp;
            <a href="#" data-bind="click: delete">Delete</a>
        </p>
    </fieldset>
}

由于我从来没有真正张贴的形式,由于一些未知的,尽管我的模型的所有属性被打上了要求属性,我看不出有任何客户端验证的迹象。我必须做什么来触发此验证我的点击Save按钮时?

Because I'm never actually posting the form, and due to some unknowns, despite all properties on my Person model being marked with the Required attribute, I see no sign of client side validation. What must I do to trigger this validation when my save button is clicked?

推荐答案

假设你的窗体有一个一流的'主':

suppose your form has a class 'main':

    $('form').submit(function() {
            var $form = $('form.main');
            $form.valid();
    });

这篇关于我如何编程触发客户端验证通过的Razor视图设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆