Step 1:
Create a New View Page For Create (Form) (Exsmple. Enter Name, Age, Mobile)
@model Students.Models.Student
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Student</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Step 2:
Create Action Create In The Controller
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Student std)
{
return View();
}
Step 3:
go to the home controller
do comment out to the
//private readonly ILogger<HomeController> _logger;
//public HomeController(ILogger<HomeController> logger)
//{
// _logger = logger;
//}
and paste this code in the Class HomeController : Controller
public class HomeController : Controller
{
private readonly StudentDbContext studentDB;
public HomeController(StudentDbContext StudentDB)
{
studentDB = StudentDB;
}
step 5:
open the controller and add this code in create(Student std)
if (ModelState.IsValid)
{
studentDB.Student.Add(std);
studentDB.SaveChanges();
return RedirectToAction("Index");
}