加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.NET MVC 4 AJAX提交表单不工作

发布时间:2020-12-15 23:48:15 所属栏目:asp.Net 来源:网络整理
导读:我试图使用MVC 4中的Ajax.BeginForm帮助方法将窗体从同步转换为异步. 在我看来,我有: @{ ViewBag.Title = "Users Edit"; var options = new AjaxOptions() { Url = Url.Action("Edit","User"),LoadingElementId = "saving",LoadingElementDuration = 2000,C
我试图使用MVC 4中的Ajax.BeginForm帮助方法将窗体从同步转换为异步.

在我看来,我有:

@{
        ViewBag.Title = "Users > Edit";
        var options = new AjaxOptions()
                          {
                              Url = Url.Action("Edit","User"),LoadingElementId = "saving",LoadingElementDuration = 2000,Confirm = "Are you sure you want to save this User?"
                          };  
    }

    <div id="saving" style="display:none; color:Red; font-weight: bold"> 
         <p>Saving...</p> 
    </div> 

    @using (Ajax.BeginForm(options)) 
    {
        @Html.ValidationSummary(true)
        <fieldset>
            .... FIELDS ...
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>
    }

当点击提交按钮时,正在执行完整的回发.我的动作方法如下所示:

[HttpPost]
public ActionResult Edit(User user)
{
    if (ModelState.IsValid)
    {
        _repository.Save(user);
        TempData["message"] = String.Format("{0} has been saved.",user.Username);
    }

    return View(user);
 }

有没有我失踪的东西? MVC 4目前的版本有几个问题吗?

在我的Web.Config我确实有ClientValidationEnabled和UnobtrusiveJavaScriptEnabled设置为true.

我也在我的_Layout.cshtml中指定了这些:

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

解决方法

Is there something I’m missing?

也许你错过了不引人注意的ajax脚本:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读