Create a new MVC 4 Application, given below-


Create a table, using the script, given below (if needed,as table will be generated on code first)-
- CREATE TABLE [dbo].[StudentDetails] (
- [Id] INT IDENTITY (1, 1) NOT NULL,
- [StudentName] NVARCHAR (MAX) NULL,
- [Age] INT NULL,
- CONSTRAINT [PK_dbo.StudentDetails] PRIMARY KEY CLUSTERED ([Id] ASC)
- );
- <connectionStrings>
- <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-BootstrapApp-20160910224720;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-BootstrapApp-20160910224720.mdf" providerName="System.Data.SqlClient" />
- </connectionStrings>
- public class StudentDetails
- {
- public int Id { get; set; }
- public string StudentName { get; set; }
- public int? Age { get; set; }
- }
- public class UsersContext : DbContext
- {
- public UsersContext()
- : base("DefaultConnection")
- {
- }
- public DbSet<StudentDetails> studentDbset { get; set; }
- }
- public class StudentController : Controller
- {
- private UsersContext db = new UsersContext();
- //
- // GET: /Student/Student/
- public ActionResult Index()
- {
- return View(db.studentDbset.ToList());
- }
- public ActionResult Index1()
- {
- return View(db.studentDbset.ToList());
- }
- //
- // GET: /Student/Student/Details/5
- public ActionResult Details(int id = 0)
- {
- StudentDetails studentdetails = db.studentDbset.Find(id);
- if (studentdetails == null)
- {
- return HttpNotFound();
- }
- return View(studentdetails);
- }
- //
- // GET: /Student/Student/Create
- [HttpGet]
- public ActionResult Create()
- {
- return View();
- }
- //
- // GET: /Student/Student/Create
- public ActionResult CreateForm()
- {
- return View();
- }
- //
- // POST: /Student/Student/Create
- [HttpPost]
- public ActionResult Create(StudentDetails studentdetails)
- {
- if (ModelState.IsValid)
- {
- db.studentDbset.Add(studentdetails);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(studentdetails);
- }
- //
- // GET: /Student/Student/Edit/5
- public ActionResult Edit(int id = 0)
- {
- StudentDetails studentdetails = db.studentDbset.Find(id);
- if (studentdetails == null)
- {
- return HttpNotFound();
- }
- return View(studentdetails);
- }
- public ActionResult EditForm(int id = 0)
- {
- StudentDetails studentdetails = db.studentDbset.Find(id);
- if (studentdetails == null)
- {
- return HttpNotFound();
- }
- return View(studentdetails);
- }
- //
- // POST: /Student/Student/Edit/5
- [HttpPost]
- public ActionResult Edit(StudentDetails studentdetails)
- {
- if (ModelState.IsValid)
- {
- db.Entry(studentdetails).State = EntityState.Modified;
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(studentdetails);
- }
- //
- // GET: /Student/Student/Delete/5
- public ActionResult Delete(int id = 0)
- {
- StudentDetails studentdetails = db.studentDbset.Find(id);
- if (studentdetails == null)
- {
- return HttpNotFound();
- }
- return View(studentdetails);
- }
- //
- // POST: /Student/Student/Delete/5
- [HttpPost, ActionName("Delete")]
- public ActionResult DeleteConfirmed(int id)
- {
- StudentDetails studentdetails = db.studentDbset.Find(id);
- db.studentDbset.Remove(studentdetails);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- protected override void Dispose(bool disposing)
- {
- db.Dispose();
- base.Dispose(disposing);
- }
- }





Humayun Kabir MamunPosted Sep 21, 2016, 12:57 AM
Nice...
Ganesan APosted Sep 19, 2016, 12:56 PM
Thanks Sathish
Satish Kumar VadlavalliPosted Sep 19, 2016, 7:16 AM
Nice explanation, keep up good work