简体   繁体   English

在mvc4中使用ajax.actionlink会刷新页面

[英]Using ajax.actionlink in mvc4 causes to refresh the page

I have a view like this: 我有这样的看法:

@model IEnumerable<DomainClasses.Class>

@{
    ViewBag.Title = "لیست ";
}
<div id="test">
<h2>لیست کلاس ها</h2>

<p>
    @Html.ActionLink("ایجاد کلاس جدید", "Create")
</p>
<table>
    <tr>
        <th>
           @Html.DisplayNameFor(model => model.ClassName)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ClassName)
        </td>
        <td>
            @*@Html.ActionLink("ویرایش", "Edit", new { id=item.Id }) |*@
            @Ajax.ActionLink("ویرایش1","Edit",new { id=item.Id },new AjaxOptions(){UpdateTargetId = "test",InsertionMode = InsertionMode.Replace,HttpMethod = "GET"})
            @Html.ActionLink("حذف", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table>
    </div>

As you can see I created a div named test , and I make an ajax action link: 如您所见,我创建了一个名为testdiv ,并创建了一个ajax操作链接:

  @Ajax.ActionLink("ویرایش1","Edit",new { id=item.Id },new AjaxOptions(){UpdateTargetId = "test",InsertionMode = InsertionMode.Replace,HttpMethod = "GET"})

So when I click on the link that I created by ajax actionlink my page is refreshed and my url changed to localhost/Class/Edit/1 . 因此,当我单击由ajax actionlink创建的链接时,页面会刷新,并且URL更改为localhost / Class / Edit / 1 Why does this happen? 为什么会这样? I thought my page shouldn't be refreshed! 我以为我的页面不应该刷新!

Best regards 最好的祝福

Make sure that you have jquery.unobtrusive-ajax.js script included on your view. 确保视图中包含jquery.unobtrusive-ajax.js脚本。 If you don't here is the nuget package http://www.nuget.org/packages/jQuery.Ajax.Unobtrusive/2.0.20710 . 如果您不在这里,则是nuget包http://www.nuget.org/packages/jQuery.Ajax.Unobtrusive/2.0.20710 When you install the package make sure that jquery.unobtrusive-ajax.js is included in your view. 安装软件包时,请确保视图中包含jquery.unobtrusive-ajax.js。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM