我使用Ajax.BeginForm将数据插入数据库,但是有问题! 当我在插入数据后单击插入按钮时,layout.cshtml中的某些元素重复,请参阅下面的图片以更好地理解它 sample image
这是模型
public class student
{
[Key]
public int studentID { get; set; }
[Required]
public string studentName { get; set; }
}
这是studentController的创建动作
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "studentID,studentName")] student student)
{
if (ModelState.IsValid)
{
db.students.Add(student);
db.SaveChanges();
//return RedirectToAction("Index");
}
return View(student);
}
这是视图
@model ajaxbegin.Models.student
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
<div id="ajaxBeginDiv">
@using (Ajax.BeginForm("Create", "students", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "ajaxBeginDiv" }))
{ @Html.AntiForgeryToken() <div class="form-horizontal">
<h4>student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.studentName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.studentName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.studentName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
</div>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
这是布局页面(默认为asp.net mvc _layout.cshtml)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data- toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
@Html.Partial("_LoginPartial")
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
你能告诉我如何解决这个问题吗?