开发者

Fire ASP.NET MVC2 client side validation from an anchor tag

开发者 https://www.devze.com 2023-03-11 01:57 出处:网络
I\'m changing an existing ASP.NET MVC2 application which uses the MicrosoftMvcValidation.js etc. for client side validation.

I'm changing an existing ASP.NET MVC2 application which uses the MicrosoftMvcValidation.js etc. for client side validation.

<h2>Details</h2>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>

<% Html.EnableClientValidation(); %>

<% using (Html.BeginForm("Details", "Add", FormMethod.Post, new { id="addForm" }))
   { %>
   开发者_高级运维 <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Title) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Title) %>
            <%: Html.ValidationMessageFor(model => model.Title) %>
        </div>
    ...
    <input type="submit" value="Create" />

The client side validation is working fine and dandy.

However, the designer wants to change the submit button to be a pretty anchor tag. I'm trying to submit the form using the anchor's onclick event i.e:

<a class="add-campaign-button-done" onclick="document.getElementById('addForm').submit();"><span></span></a>

The form submits, but client-side validation no longer works.


With a little bit of jQuery you can do the trick, see this post : Call MVC 3 Client Side Validation Manually for ajax posts for more informations and let me know if you need more help!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号