How can I programmatically trigger client side val

2019-04-05 11:06发布

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>

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>
}

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?

1条回答
ら.Afraid
2楼-- · 2019-04-05 11:28

suppose your form has a class 'main':

    $('form').submit(function() {
            var $form = $('form.main');
            $form.valid();
    });
查看更多
登录 后发表回答