Ajax.beginform执行完全回发而不是部分回发
在一个视图中我有一个像下面的ajaxforms:
@using (Ajax.BeginForm("ShowPartial", new AjaxOptions() { InsertionMode=InsertionMode.Replace, UpdateTargetId="dane" })) { // My menu here } @Html.Partial("ShowPartial")
ShowPartial与此控制器方法连接:
public ActionResult ShowPartial(string DeviceName, string submit, int? Page) { List<Expression<Func>> where = new List<Expression<Func>>(); 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:
@model IEnumerable //table headers @foreach (var item in Model) { // rendering table contet }
任何人都可以告诉我为什么上面的代码是完全回发而不是部分。
检查这个工作方案 –
让你的主控制器动作如下所示 –
public ActionResult Ajax() { return View(); }
它将返回以下View。 我已经安装了Unobstructive Ajax nuget并引用了它的js脚本,如下所示。
@{ ViewBag.Title = "Ajax"; } Ajax
@using (Ajax.BeginForm("ShowPartial", new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = "dane" })) { } @Html.Action("ShowPartial")
然后我还有一个控制器动作,当这个ajax请求发出时会被击中。 此控制器操作将返回您的部分视图 –
public ActionResult ShowPartial() { return PartialView("MyPartial"); }
局部视图如下 –
@DateTime.Now
当您运行应用程序并单击按钮时,日期时间将在div中更改而不会回发 –
我不知道微软的ajax助手依赖于Jquery + Microsoft jquery插件。 在意识到我找到了需要的插件并使用包管理器下载它之后。
PM> Install-Package Microsoft.jQuery.Unobtrusive.Ajax