我正在尝试使用局部视图中包含的ajax表单更新ASP.NET MVC中的局部视图。但是,无论何时提交表单,整个页面都会更改为局部视图,而不仅仅是包含局部视图的页面部分。
我的部分查看表格:
@using (Ajax.BeginForm("Archive", "Products", new { id = note.ID },
new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.ReplaceWith,UpdateTargetId = @*Unique ID*@ }))
{
@Html.AntiForgeryToken()
<input type="submit" value="Archive" class="btn btn-default"/>
}
它调用的函数:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Archive(int ID)
{
ProductNote model = db.ProductNotes.FirstOrDefault(x => x.ID == ID);
model.Archived = true;
db.SaveChanges();
Product modelP = db.Products.FirstOrDefault(z => (z.ProductNotes.FirstOrDefault(y => y.ID == model.ID).NoteText != null));
System.Diagnostics.Debug.WriteLine(modelP.ID.ToString() + "Test Test 123");
System.Diagnostics.Debug.WriteLine("Test\n\n\n");
return PartialView("_ProductNotes", modelP);
}
答案 0 :(得分:0)
我认为你应该尝试将你的Patialview放在带有id
的div标签中<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<div id="replace-this">
@using (Ajax.BeginForm("Archive", "Products", new { id = note.ID },
new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.ReplaceWith,UpdateTargetId = "replace-this" }))
{
@Html.AntiForgeryToken()
<input type="submit" value="Archive" class="btn btn-default"/>
}
</div>
答案 1 :(得分:0)
在部分视图底部添加对以下jquery库的引用:
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>