Ajax.beginform执行完全回发,而不是局部的局部、而不是、Ajax、beginform

2023-09-11 00:59:28 作者:仅存的骄傲ゝ

在一个视图我有阿贾克斯的形式如下图所示:

In a view I have ajax form like below:

@using (Ajax.BeginForm("ShowPartial", new AjaxOptions()
{
    InsertionMode=InsertionMode.Replace,
    UpdateTargetId="dane"
}))
{
  // My menu here
}
@Html.Partial("ShowPartial")

ShowPartial与此控制器的连接方法:

ShowPartial is connected with this controller method:

    public ActionResult ShowPartial(string DeviceName, string submit, int? Page)
    {
        List<Expression<Func<DeviceInstance, bool>>> where = new List<Expression<Func<DeviceInstance, bool>>>();
        int PageNo = 1;
        if (Page.HasValue)
        {
            PageNo = Page.Value;
        }
        if (DeviceName != "" && DeviceName != null)
        {
            where.Add(w => w.Device.Name.Contains(DeviceName));
        }
        return PartialView(unitOfWork.deviceInstanceRepository.Get(where, q => q.OrderBy(o => o.Id), PageNo, w => w.Device, w => w.DeviceUsage));
    }

和PartialView:

And PartialView:

@model IEnumerable<magazyn.Models.DeviceInstance>
<table class="table table-striped" id="dane">
    <tr>
        //table headers
    </tr>

    @foreach (var item in Model)
    {
        // rendering table contet
    }

</table>

谁能告诉我为什么code以上是使部分完全回发来代替。

Can anyone tell me why code above is making full postback instead of partial.

推荐答案

勾选此工作的解决方案 -

Check this working solution -

让你的主控制器的动作是如下 -

Let your main Controller action be as shown below -

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

和它将返回下面的视图。我已经安装了无障碍传输阿贾克斯的NuGet,并引用了JS脚本如下图所示

And it will return following View. I have installed Unobstructive Ajax nuget and referenced its js script as shown below.

@{
    ViewBag.Title = "Ajax";
}

<h2>Ajax</h2>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

@using (Ajax.BeginForm("ShowPartial", new AjaxOptions()
{
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "dane"
}))
{
    <input type="submit" value="click"/>
}

<div id="dane">
    @Html.Action("ShowPartial")
</div>

然后,我又多了一个控制器操作,将得到击中时,这个AJAX请求。该控制器行动将回报您的局部视图 -

Then I have one more controller action which will get hit when this ajax request is made. This controller action would return your partial view -

    public ActionResult ShowPartial()
    {
        return PartialView("MyPartial");
    }

和局部视图如下 -

And the partial view is as follows -

<div>
    @DateTime.Now
</div>

当你运行应用程序并点击按钮,日期时间将在DIV没有回发改 -

And when you run the application and click on the button, datetime will be changed in the div without a postback -