Asp.Net

Tuesday 12 August 2014

MVC3 Application Using Code First Approach

Model:Student.cs



using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.ComponentModel.DataAnnotations;

using System.Data.Entity;

namespace MVC_C.Models

{

    public class Student

    {

        [Key]

        public int S_Id { get; set; }

        [Required]

        [StringLength(30)]

        public string S_Name { get; set; }

        [Required]

        [StringLength(30)]

        public string S_Class { get; set; }

        [Required]

        [StringLength(30)]

        public string S_Grade { get; set; }

        [Required]

        [StringLength(10)]

        public string S_Mob { get; set; }

    }

    public class Studentcontext : DbContext

    {

        public DbSet<Student> Students { get; set; }

    }

}


Controller:StudentController.cs


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using MVC_C.Models;

using System.Data.Entity;

namespace MVC_C.Controllers

{

    public class StudentController : Controller

    {

        Studentcontext db = new Studentcontext();

        //

        // GET: /Student/

        public ActionResult Index()

        {

            return View(db.Students.ToList());

        }

        //

        // GET: /Student/Details/5

        public ActionResult Details(int id)

        {

        

           return View(db.Students.Find(id));

        }

        //

        // GET: /Student/Create

        public ActionResult Create()

        {

            return View();

        }

        //

        // POST: /Student/Create

        [HttpPost]

        public ActionResult Create(Student student)

        {

            try

            {

                // TODO: Add insert logic here

                db.Students.Add(student);

                db.SaveChanges();

                return RedirectToAction("Index");

            }

            catch

            {

                return View();

            }

        }

      

        //

        // GET: /Student/Edit/5

        public ActionResult Edit(int id)

        {

          

            return View(db.Students.Find(id));

        }

        //

        // POST: /Student/Edit/5

        [HttpPost]

        public ActionResult Edit(Student student)

        {

            try

            {

                db.Entry(student).State= System.Data.EntityState.Modified;

                db.SaveChanges();

          

            return RedirectToAction("Index");

            }

            catch

            {

                return View();

            }

        }

        //

        // GET: /Student/Delete/5

        public ActionResult Delete(int id)

        {

            return View(db.Students.Find(id));

        }

        //

        // POST: /Student/Delete/5

        [HttpPost]

        public ActionResult Delete(Student student)

        {

            try

            {

                db.Entry(student).State = System.Data.EntityState.Deleted;

                db.SaveChanges();

                return RedirectToAction("Index");

            }

            catch

            {

                return View();

            }

        }

    }

}


View:Student/Index.cshtml(while adding view you should select list template)


@model IEnumerable<MVC_C.Models.Student>

@{

    ViewBag.Title = "Index";

}

<h2>Index</h2>

<p>

    @Html.ActionLink("Create New", "Create")

</p>

<table>

    <tr>

        <th>

            S_Name

        </th>

        <th>

            S_Class

        </th>

        <th>

            S_Grade

        </th>

        <th>

            S_Mob

        </th>

        <th></th>

    </tr>

@foreach (var item in Model) {

    <tr>

        <td>

            @Html.DisplayFor(modelItem => item.S_Name)

        </td>

        <td>

            @Html.DisplayFor(modelItem => item.S_Class)

        </td>

        <td>

            @Html.DisplayFor(modelItem => item.S_Grade)

        </td>

        <td>

            @Html.DisplayFor(modelItem => item.S_Mob)

        </td>

        <td>

            @Html.ActionLink("Edit", "Edit", new { id=item.S_Id }) |

            @Html.ActionLink("Details", "Details", new { id=item.S_Id }) |

            @Html.ActionLink("Delete", "Delete", new { id=item.S_Id })

        </td>

    </tr>

}

</table>



No comments:

Post a Comment