I used to develop some applications in C#/Winforms and now I'm trying to learn web development with MVC.
It's a lot of new concepts (JS,Ajax,ASP.net, etc...), so please be tolerent...
I have read a lot and began to work and try, but there is a point that I still don't understand.
For exemple, I want to insert datas in a database via a Form, just insert, nothing else.
The method to insert is in the controller.
The evident method is to use Html helper @Html.BeginForm
. At this step I'm not able to do that without reloading the whole page.
Due to the fact that I didn't find any clear answer, could you please help me.
1-Is it possible with @Html.BeginForm
to do nothing after posting form in order to insert in database (maybe with a specific return type of the action in the controller) or Ajax.BeginForm
is the unique solution ?
2-Same to update a part of a page, is ajax the unique solution ?
Many thanks.
I have create one form in asp.net core with post data using ajax It's working Create Partial View
@model Department
<h2>Create</h2>
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
<div class="form-horizontal">
<form asp-action="Create" role="form" asp-controller="Department" asp-antiforgery="true"
data-ajax-success="Bindgrid"
data-ajax="true"
data-ajax-method="POST">
<div class="form-group">
<label class="control-label" asp-for="DepartmentName"></label>
<input class="form-control" type="text" asp-for="DepartmentName" />
<span asp-validation-for="DepartmentName" class="text-danger"></span>
</div>
<input class="btn btn-primary" type="submit" value="Save" />
</form>
</div>
Controller logic
[ValidateAntiForgeryToken()]
[HttpPost]
public JsonResult Create(Department department)
{
if (ModelState.IsValid)
{
Context.Department.Add(department);
Context.SaveChanges();
}
return Json(department);
}