Ajax.BeginForm不会触发AJAX脚本,落后于回发

前端之家收集整理的这篇文章主要介绍了Ajax.BeginForm不会触发AJAX脚本,落后于回发前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我会更新这个问题的解决方案,我想包括我的问题和解决方案,因为我无法找到它在Stackoverflow.如果你想与解决方案一起跳跃,请随时随地.

我有一个新创建的使用Visual Studio 2013创建的“空”MVC5项目.如果可能,我需要一个来自并希望的AJAX行为.使用Ajax.BeginForm在MVC3中始终是直截了当的,所以我认为它也将在MVC5中.

但是,当我单击提交按钮时,不会调用在AjaxOptions中的OnBegin,OnFailure或OnSuccess中指定的JavaScript函数.相反,Ajaxless的帖子被发送到服务器,这通过检查返回false的Request.IsAjaxRequest来证明是正确的.

我得出结论,由于某种原因,那么,ajax根本就没有被使用.我检查了一些事情,如web.config,我的布局脚本等.

我的布局包括以下脚本:

<script src="~/Scripts/jquery-1.5.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

我的web.config包括

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
</configuration>

我的看法:

@model ContactEnquiryviewmodel

@{
AjaxOptions ContactOptions = new AjaxOptions()
{
    OnBegin = "OnConactFormBegin()",OnSuccess = "OnContactFormSuccess()",OnFailure = "OnContactFormFailure()"
    };
}

<div id="EnquiryFormContainer">

@using (Ajax.BeginForm("Contact","Home",new { formName = "EnquiryForm" },ContactOptions))
    {
    @Html.HiddenFor(m => m.Subject)

    <div class="field">
        @Html.LabelFor(m => m.FirstName)
        @Html.TextBoxFor(m => m.FirstName)
        <div class="validation">
            @Html.ValidationMessageFor(m => m.FirstName)
        </div>
    </div>

    <div class="field">
        @Html.LabelFor(m => m.LastName)
        @Html.TextBoxFor(m => m.LastName)
        <div class="validation">
            @Html.ValidationMessageFor(m => m.LastName)
        </div>
    </div>

    <div class="field">
        @Html.LabelFor(m => m.Email)
        @Html.TextBoxFor(m => m.Email)
        <div class="validation">
            @Html.ValidationMessageFor(m => m.Email)
        </div>
    </div>

    <div class="field">
        @Html.LabelFor(m => m.PhoneNumber)
        @Html.TextBoxFor(m => m.PhoneNumber)
        <div class="validation">
            @Html.ValidationMessageFor(m => m.PhoneNumber)
        </div>
    </div>

    <div class="field">
        @Html.LabelFor(m => m.Comments)
        @Html.TextAreaFor(m => m.Comments)
        <div class="validation">
            @Html.ValidationMessageFor(m => m.Comments)
        </div>
    </div>

    <div id="submitButtonContainer">
        <input type="submit" value="Submit" name="submit" />
    </div>
    }
</div>

我的控制器动作(未完成):

[HttpPost]
public ActionResult Contact(Contactviewmodel viewmodel,String formName = "")
    {
    if (Request.IsAjaxRequest())
        {
        return new EmptyResult();               
        }

    return new EmptyResult();
    }

我已经检查了一些关于这个问题的其他帖子,找不到解决方案.虽然有人暗示了可能的解决方案,但我对Microsoft CDN(http://www.asp.net/ajaxlibrary/cdn.ashx)感到困惑.

在Microsoft CDN上,他们有以下部分:

以下ASP.NET MVC JavaScript文件托管在此CDN上:

ASP.NET MVC 5.0

http://ajax.aspnetcdn.com/ajax/mvc/5.0/jquery.validate.unobtrusive.js
http://ajax.aspnetcdn.com/ajax/mvc/5.0/jquery.validate.unobtrusive.min.js

ASP.NET MVC 4.0

http://ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.js
http://ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js

ASP.NET MVC 3.0

http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.unobtrusive-ajax.js
http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.unobtrusive-ajax.min.js
http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.js
http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js
http://ajax.aspnetcdn.com/ajax/mvc/3.0/MicrosoftMvcAjax.js
http://ajax.aspnetcdn.com/ajax/mvc/3.0/MicrosoftMvcAjax.debug.js 

ASP.NET MVC 2.0

http://ajax.aspnetcdn.com/ajax/mvc/2.0/MicrosoftMvcAjax.js
http://ajax.aspnetcdn.com/ajax/mvc/2.0/MicrosoftMvcAjax.debug.js 

ASP.NET MVC 1.0

http://ajax.aspnetcdn.com/ajax/mvc/1.0/MicrosoftMvcAjax.js
http://ajax.aspnetcdn.com/ajax/mvc/1.0/MicrosoftMvcAjax.debug.js

他们似乎建议MVC5需要的唯一脚本是jquery.validate.unobtrusive.

获取本地副本作为您的MVC项目的一部分“ 从VS2013,右键单击您的MVC 5项目,选择“管理NuGet软件包”. 选择“在线”并搜索“jquery.unobtrusive-ajax”,然后安装“Microsoft.jQuery.Unobstusive.Ajax”.

猜你在找的Ajax相关文章