C# Ajax.beginform在提交时会在asp.net mvc的数据库中创建多个条目
我在create form中使用ajax.beginform,当我提交表单时,它会在数据库中创建多个条目,第一次创建3个条目,第二次提交6行,以此类推 以下是我的索引视图:C# Ajax.beginform在提交时会在asp.net mvc的数据库中创建多个条目,c#,asp.net-mvc,C#,Asp.net Mvc,我在create form中使用ajax.beginform,当我提交表单时,它会在数据库中创建多个条目,第一次创建3个条目,第二次提交6行,以此类推 以下是我的索引视图: @model IEnumerable<CourseSelection.Models.country> @{ Layout = Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/parameterview.cshtml"; } @Scri
@model IEnumerable<CourseSelection.Models.country>
@{
Layout = Layout = Request.IsAjaxRequest() ? null : "~/Views/Shared/parameterview.cshtml";
}
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
<script src="~/js/main/jquery.validate.js"></script>
<div class="btn-danger ma ci" style="height:50px ;">
<h2 class="white" style=" float:left; margin-left:5px ; margin-top:2px; margin-bottom:10px;"> COUNTRIES </h2>
<button id="sh" class="btn btn-primary" style="float:right; margin-right:5px;">
@Ajax.ActionLink(
"Create",
"Create",
"countries",
new AjaxOptions
{
UpdateTargetId = "EditDivId",
InsertionMode = InsertionMode.ReplaceWith
})
</button>
</div>
<div id="EditDivId">
</div>
<div id="indexdiv">
<div class="nicdark_space10"></div>
@{ Html.RenderAction("_i"); }
</div>
这是因为
@Scripts.Render(“~/Scripts/jquery.unobtrusive ajax.min.js”)
不止一个。如果已在布局页(母版页)中呈现脚本部分,请从“创建部分”视图甚至其他视图中删除这些脚本部分
重要提示:
如果这些脚本已经在布局页面(母版页)中呈现,那么在其他任何地方都不需要这些脚本
说明:
以下是将jquery.unobtrusive-ajax.js放入页面时将运行的部分代码:
$(function(){
//...
$(document).on("submit", "form[data-ajax=true]", function (evt) {
var clickInfo = $(this).data(data_click) || [],
clickTarget = $(this).data(data_target),
isCancel = clickTarget && clickTarget.hasClass("cancel");
evt.preventDefault();
if (!isCancel && !validate(this)) {
return;
}
asyncRequest(this, {
url: this.action,
type: this.method || "GET",
data: clickInfo.concat($(this).serializeArray())
});
});
//...
});
因此,每次渲染该文件时,此事件处理程序都会附加到您的“提交”按钮。您是否检查了答案?@RezaAghaei感谢它终于可以工作了,非常感谢
public class countriesController : Controller
{
private CourseSelectionEntities3 db = new CourseSelectionEntities3();
// GET: countries
public ActionResult Index()
{
return View(db.countries.ToList());
}
public PartialViewResult _i()
{
return PartialView("_i", db.countries.ToList());
}
// GET: countries/Create
public PartialViewResult Create()
{
return PartialView("_c");
}
// POST: countries/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,countryName,active")] country country)
{
if (ModelState.IsValid)
{
country.countryName = country.countryName.ToUpper();
db.countries.Add(country);
db.SaveChanges();
return View("Index", db.countries.ToList());
}
return PartialView(country);
}
}
$(function(){
//...
$(document).on("submit", "form[data-ajax=true]", function (evt) {
var clickInfo = $(this).data(data_click) || [],
clickTarget = $(this).data(data_target),
isCancel = clickTarget && clickTarget.hasClass("cancel");
evt.preventDefault();
if (!isCancel && !validate(this)) {
return;
}
asyncRequest(this, {
url: this.action,
type: this.method || "GET",
data: clickInfo.concat($(this).serializeArray())
});
});
//...
});