开发者

Conditionally cancelling an ASP.NET MVC 2 form post

I am trying to get some javascript to run before the form is submitted back to the server. In this javascript I will either开发者_如何学Go allow the post or not based on some validation logic in the javascript function. I have read elsewhere that to keep the form post from happening all I need to do is return false from my javascript function. I am trying that in the code below with no luck. I added the alert to prove to myself the function was being called. Can anyone tell me what I am doing wrong?

<script type="text/javascript">
function validateOtherAndComment() {
    alert("validateOtherAndComment called");
    return false;
}
</script>
<h2>Create</h2>

<%
    using (Html.BeginForm("Create", "Home", FormMethod.Post,
                          new
                              {
                                  onsubmit = "validateOtherAndComment();",
                              }))
    {%>
    <%=Html.ValidationSummary(true)%>

    <fieldset>
        <legend>New Event</legend>

        <div class="editor-label">
            <%=Html.Label("Comment (max 250)")%>
        </div>
        <div class="editor-field">
            <%=Html.TextArea("commentTextArea", "", 7, 40, new object())%>
            <%=Html.ValidationMessage("comment")%>
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<%
    }%>


Try like this:

using (Html.BeginForm(
    "Create", 
    "Home", 
    FormMethod.Post,
    new {
        onsubmit = "return validateOtherAndComment();"
    }))

Notice the return statement.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜