سلام
من کد زیر را نوشته ام
@model Modal.Models.TestModal @{ ViewBag.Title = "Create"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>TestModal</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Family, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Family, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Family, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" onclick="Insert();" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div> @section scripts{ <script> function Insert() { $.ajax({ url: "@Url.Action("Create", "TestModals1")", type: "Post", data: {} }).done(function (res) { alert(1); }); } </script> }
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "Id,Name,Family,Age")] TestModal testModal) { if (ModelState.IsValid) { db.TestModals.Add(testModal); db.SaveChanges(); return RedirectToAction("Index"); } return View(testModal); }
من می خواهم عمل ثبت بکمک ajax انجام شود
و اسکریپت زیر را نوشته ام
<script> function Insert() { $.ajax({ url: "@Url.Action("Create", "TestModals1")", type: "Post", data: {} }).done(function (res) { alert(1); }); } </script>
می دانم اشتباه است
من میخواهم وقتی روی دکمه ثبت کلیک کردم کد بالا اجرا شود وقتی درست عملیات ثبتاجرا شد به جای ارجا به صفحه index مثلا به صفحه جزییات برود
نمی دانم کدم را چگونه بنویسم
پیشنهاد ممیکنم از Ajax.BeginForm استفاده کنید
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)