【问题标题】:How to prevent postback in beginform asp.net mvc如何防止beginform asp.net mvc中的回发
【发布时间】:2015-06-10 18:34:40
【问题描述】:

在我看来,我有一个 beginform,它有两个按钮,一个用于保存,一个用于取消。单击保存时,它将验证表单控件中的数据,包括客户端和服务器端,并回发到控制器。问题是当我单击保存或取消时,它会尝试验证数据并显示验证摘要。但是,我想在单击取消按钮时阻止回发和验证。在取消按钮中,我正在调用一个名为 Cancel() 的 JavaScript 函数,该函数调用 window.history.back(-1);它仍在尝试回发。以下是我的代码。如果我将取消按钮移出表单,则它不会进行回发,但是样式会混乱,因为我希望保存和取消按钮并排

 <div id="updateusercreds" class="items">
                    @using (Html.BeginForm("SaveCustomer", "NewCustomer"))
                    {
                     <div>
                        @Html.LabelFor(x => x.Address.Email)
                        <span>
                            @Html.Kendo().TextBoxFor(model => model.Address.Email)
                        </span>
                    </div>
                    <div>
                    @Html.LabelFor(x => x.Address.Company)
                    <span>
                        @Html.Kendo().TextBoxFor(model => model.Address.Company)
                    </span>
                    </div>

                    <div>
                        @Html.LabelFor(x => x.Address.StreetAddress1, "Street Address")
                        <span>
                            @Html.Kendo().TextBoxFor(model => model.Address.StreetAddress1)
                            <br/>
                            @Html.Kendo().TextBoxFor(model => model.Address.StreetAddress2)
                        </span>
                    </div>

                    <div>
                    @Html.LabelFor(x => x.Address.Fax)
                    <span>
                        @Html.Kendo().TextBoxFor(model => model.Address.Fax)
                    </span>
                    </div>
                    @Html.ValidationSummary()
                    @Html.TradeUI().NotificationSummary()

                    <div class="tti-actions">
                        <button type="submit">Save</button> 
                        <button onclick="javascript:Cancel();">Cancel</button>                             
                    </div>
                    }

                </div>

【问题讨论】:

    标签: javascript asp.net-mvc


    【解决方案1】:

    设置按钮类型为button

    <button type="button" onclick="javascript:Cancel();">Cancel</button>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 2012-08-29
      • 2011-05-03
      相关资源
      最近更新 更多