Jquery 在ajax插入之后,加载所有插入的列表而不重新加载页面
我有一个有几个不同标签的表单。主选项卡包含有关此人的所有主要信息,其余选项卡由多个列表组成 其中一个选项卡列出了与此人相关的所有评论。该选项卡还有一个按钮,允许用户插入新注释 我要做的是调用ajax并加载create视图,用视图替换列表。像这样的Jquery 在ajax插入之后,加载所有插入的列表而不重新加载页面,jquery,ajax,razor,asp.net-mvc-5,Jquery,Ajax,Razor,Asp.net Mvc 5,我有一个有几个不同标签的表单。主选项卡包含有关此人的所有主要信息,其余选项卡由多个列表组成 其中一个选项卡列出了与此人相关的所有评论。该选项卡还有一个按钮,允许用户插入新注释 我要做的是调用ajax并加载create视图,用视图替换列表。像这样的 $("#btnNewAnnotation").click(function (e) { $.ajax({ url: "/Annotations/Create", cache: false,
$("#btnNewAnnotation").click(function (e) {
$.ajax({
url: "/Annotations/Create",
cache: false,
data: { personId: $("#personId").val(), withouLayout: true },
type: "GET",
success: function (data, textStatus, jqXHR) {
alert("alert of success!!");
$("#annotationContent").html(data);
},
error: function (statusText, errorText) {
alert(errorText);
}
});
});
现在控制器做什么它转储一个局部视图,允许我将新的HTML放在注释内容div上
public ActionResult Create(string personId, string withouLayout)
{
ViewBag.AnnotationTypeId = new SelectList(db.annotationtypes, "AnnotationTypeId", "Name");
annotations annotation = new annotations();
annotation.PersonId = personid;
return PartialView(annotation);
}
这一切都非常好,没有任何问题。我遇到的问题是在数据库中插入注释之后。我要做的是再次重新加载包含所有注释的列表,但只更新divannotationContent
我有以下几点
$("#createAnnotationForm").on("submit", function () {
e.preventDefault();
$.ajax({
url: "/Annotations/Create",
cache: false,
data: $(this).serialize(),
dataType: "json",
type: "POST",
success: function (data, textStatus) {
//This alert never gets Called!!
alert("ajax insert success");
},
error: function (statusText, errorText) {
alert('Error: ' + errorText)
}
});
});
我这样做的原因是视图中有多个表单,我只想提交此表单。我的控制器具有以下功能
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(annotations annotations)
{
try
{
db.annotations.Add(annotations);
db.SaveChanges();
}
catch(Exception ex)
{
}
return PartialView(db.annotations);
}
当我提交表单时,我得到了错误信息
The model item passed into the dictionary is of type 'System.Data.Entity.DbSet`1[RecruitmentWeb.Models.annotations]', but this dictionary requires a model item of type 'RecruitmentWeb.Models.annotations'.
我理解为什么会出现这个错误,但我不知道如何在不重新加载整个页面的情况下重新加载我的评论列表。有人知道吗?您通常希望传入注释类的单个实例 我假设annotation是一个DbSet,annotation是您的实际类 是的
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(annotation annotation)
{
try
{
db.annotations.Add(annotation);
db.SaveChanges();
}
catch(Exception ex)
{
}
return PartialView(db.annotations);
}