PDA

View Full Version : سوال: عمل ثبت بوسیله ajax



Iran58
شنبه 10 شهریور 1397, 15:59 عصر
سلام
من کد زیر را نوشته ام


@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 انجام شود
و اسکریپت زیر را نوشته ام


@section scripts{
<script>
function Insert() {
$.ajax({
url: "@Url.Action("Create", "TestModals1")",
type: "Post",
data: {}
}).done(function (res) {
alert(1);
});


}


</script>


}


می دانم اشتباه است
من میخواهم وقتی روی دکمه ثبت کلیک کردم کد بالا اجرا شود وقتی درست عملیات ثبتاجرا شد به جای ارجا به صفحه index ادامه کار در Ajax ادامه پیدا کند
نمی دانم کدم را چگونه بنویسم

Mahmoud.Afrad
یک شنبه 11 شهریور 1397, 12:51 عصر
کنترولر و اکشن و آیدی مدل را به متد ریدایرکت ارسال کنید
https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.redirectto action?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fqu ery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(Microsoft.AspNetCore.Mvc.ControllerBase .RedirectToAction)%3Bk(DevLang-csharp)%26rd%3Dtrue&view=aspnetcore-2.1#Microsoft_AspNetCore_Mvc_ControllerBase_Redire ctToAction_System_String_System_String_System_Obje ct_