带有提交按钮的ASP.Net Mvc Ajax.BeginForm会触发两次

时间:2015-09-17 03:48:00

标签: asp.net-mvc-4 asp.net-ajax

Ajax.BeginForm("Index", new AjaxOptions(...))与提交按钮一起使用时。它调用动作方法两次。可能是什么原因?当我用Ajax.BeginForm替换Html.BeginForm时,它可以正常工作。

@using (Ajax.BeginForm(new AjaxOptions { OnSuccess = "onTemplateSaveSuccess(data);", OnComplete = "hideProgress();" }))
    {
        @Html.AntiForgeryToken()
        <input type="hidden" name="templatePageNumber" id="templatePageNumber" />
        <input type="hidden" name="templatePageSize" id="templatePageSize" />
        @Html.HiddenFor(model => model.TemplateCode)

        <div class="event-subheading">Templates </div>
        <div id="TemplateInputDiv">

            <fieldset>
                <ul>
                    <li>
                        <label>Template Name </label>
                        @Html.TextBoxFor(model => model.TemplateName)
                        @Html.ValidationMessageFor(model => model.TemplateName)
                    </li>
                    <li>
                        <label>Type</label>
                        <input type="radio" name="TemplateType" class="radio-cover" value="1" checked="checked" /><span class="radio-txt">Email&nbsp;&nbsp;&nbsp;</span> <input type="radio" name="TemplateType" class="radio-cover" value="2" /> <span class="radio-txt">SMS&nbsp;&nbsp;&nbsp;</span> <input type="radio" name="TemplateType" class="radio-cover" value="3" /><span class="radio-txt">Badge</span>
                        <div class="clearfix"></div>
                    </li>

                    <li>
                        <label>Content </label>
                        @Html.TextAreaFor(model => model.TemplateContent, 10, 40, null)
                        @Html.ValidationMessageFor(model => model.TemplateContent)
                    </li>
                    <li>
                        <label></label><a href="myModal" data-toggle="modal" data-target="#myModal"><label class="hint--success" style="color:#23527c;font-weight:bold;" data-hint="Use hand button in the Dynamic Data window to insert the dynamic data tag to the current position of the editor."><b>Dynamic Data</b></label></a>
                        <span></span>
                    </li>
                    <li>
                        <label></label>
                        <input type="submit" value="Save" id="submitTemplate" name="submitTemplate" class="sub_mit" />
                        <input type="reset" value="Cancel" class="sub_mit" id="cancel" />
                    </li>
                </ul>
            </fieldset>

            <div class="clearfix"></div>
        </div>

    }

2 个答案:

答案 0 :(得分:0)

检查 onTemplateSaveSuccess(data); 方法,也许它会再次触发ajax reuqest。

  1. 转到chrome(F12)中的netwrok选项卡并检查客户端生成的ruqest。它会告诉你为什么要做出第二次请求以及是谁做出来

答案 1 :(得分:0)

请分享onTemplateSaveSuccess方法的代码。尝试从此方法返回false并在此方法的末尾设置e.preventDefault()。如果这不起作用,那么尝试写e.stopPropagation()