asp.net MVC ajax会在我的div中回发而不是渲染局部视图

时间:2015-02-23 10:09:27

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

我的代码是用index.cshtml编写的

@Ajax.ActionLink("First", "First", new AjaxOptions()
{
    HttpMethod = "GET",
    UpdateTargetId = "partialstest",
    InsertionMode = InsertionMode.Replace
})

@Ajax.ActionLink("Second", "Second", new AjaxOptions()
{
    HttpMethod = "GET",
    UpdateTargetId = "partialstest",
    InsertionMode = InsertionMode.Replace
})

<div id="partialstest" style="width: 500px; height:500px; border:1px solid red;">

</div>

<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/additional-methods.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/mvc/5.2/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

它是我的控制者:

public ActionResult Index()
    {
        return View();
    }

    public PartialViewResult First()
    {

        return PartialView("Partial1");
    }

    public PartialViewResult Second()
    {

        return PartialView("Partial1");
    }

当我在这里调试它时,链接:

  

http://localhost:23166/switchPartial/Index

当我点击我的链接(第一个或第二个)时,我希望我的部分视图在div中呈现,其中id为 partialstest 。但相反,它会回发并进入

  

http://localhost:23166/switchPartial/First

为什么?

1 个答案:

答案 0 :(得分:1)

检查../../Scripts文件夹中是否存在 MicrosoftAjax.js MicrosoftMvcAjax.js 文件。对于MVC3及更高版本,请确保使用 jquery.unobtrusive-ajax.js