Thursday, February 26, 2026

Controller example with insert student c#

 Action method with Http post , authenticate and insert a student record in DB 


[Authorize]

[HttpPost]

Public ActionResult InserStudent(Student student)

{

if(ModelState.IsValid)

{

using(var context=new StudentContext())

{

Var entity = new StudentTbl

{

Id=student.Id,

Name=student.Name

};

context.StudentTbl.Add(entity);

context.SaveChanges();

}

return RedirectToAction("Index");

}

Return View(student);

}