【问题标题】:How to stop page change on Ajax Form Posting in MVC Mobile如何在 MVC Mobile 中停止 Ajax 表单发布的页面更改
【发布时间】:2014-05-29 06:48:26
【问题描述】:

我正在 MVC Mobile 上创建应用程序并遇到问题。在这里,我通过 ajax 在页面运行时加载了部分视图。在其中我有一个简单的表单,它将通过 ajax 请求提交,如下所示:

@model Test.Models.TestModel
@using (this.Ajax.BeginForm("Create", "Test", Model, new AjaxOptions { UpdateTargetId = "divResult", LoadingElementId = "loading", LoadingElementDuration = 2000, HttpMethod = "Post" }, new { id = "frmCreate" }))
{
    @Html.AntiForgeryToken()

    <div class="messageBox">@Html.Raw(TempData["Message"])</div>
    <div class="atmForm">
        <div class="control-group">
            @Html.LabelFor(x => x.Name)
            @Html.TextBoxFor(x => x.Name, new { @placeholder = "Name", @class = "inputStyle" }) @Html.ValidationMessageFor(x => x.Name)
        </div>
        <div class="control-group">
            @Html.LabelFor(x => x.Notes)
            @Html.TextAreaFor(x => x.Notes, new { @placeholder = "Notes", @class = "inputStyle" }) @Html.ValidationMessageFor(x => x.Notes)
        </div>
    </div>
    <div class="form-actions2 clearfix">
        <input type="submit" class="btn btn-block" value="Create" data-ajax="false" id="btnFormSubmit" />
    </div>
}
<script type="text/javascript">
    $.validator.unobtrusive.parse("#frmCreate");
</script>

问题是当用户提交表单时控制器调用了两次。第一次它返回部分视图,然后再次调用,然后 jQuery Mobile 更改页面。我正在使用 MVC4 和 jQuery Mobile 1.1.0

(请注意 create.cshtml(桌面版)工作正常,但 create.Mobile.cshtml(移动版)有这个问题)

【问题讨论】:

    标签: c# jquery ajax asp.net-mvc jquery-mobile


    【解决方案1】:

    不要提交表单,使用$("#myForm").serialize()作为ajax数据。

    【讨论】:

      猜你喜欢
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 2020-09-19
      • 1970-01-01
      • 2010-10-26
      • 2017-05-27
      • 1970-01-01
      相关资源
      最近更新 更多