Thursday, June 8, 2023

Detail View, Delete View and Create View in ASP.NET MVC Student


.......................................Model/Student........................
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace MVCDemo.Models
{
    public class Student
    {
        public int StudentId { get; set; }

        [Display(Name = "Name")]
        public string StudentName { get; set; }

        public int Age { get; set; }
    }

    public class Standard
    {
        public int StandardId { get; set; }
        public string StandardName { get; set; }
    }
}

.......................................StudentController...........................
using MVCDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCDemo.Controllers

{
    public class StudentController : Controller
    {
        static IList<Student> studentList = new List<Student>{
                new Student() { StudentId = 1, StudentName = "John", Age = 18 } ,
                new Student() { StudentId = 2, StudentName = "Steve",  Age = 21 } ,
                new Student() { StudentId = 3, StudentName = "Bill",  Age = 25 } ,
                new Student() { StudentId = 4, StudentName = "Ram" , Age = 20 } ,
                new Student() { StudentId = 5, StudentName = "Ron" , Age = 31 } ,
                new Student() { StudentId = 4, StudentName = "Chris" , Age = 17 } ,
                new Student() { StudentId = 4, StudentName = "Rob" , Age = 19 }
            };

        // GET: Student
        public ActionResult Index()
        {
            //fetch students from the DB 

            return View(studentList.OrderBy(s => s.StudentId).ToList());
        }
        public ActionResult Edit(int id)
        {
            //Get the student from studentList sample collection for demo purpose.
            //You can get the student from the database in the real application
            var std = studentList.Where(s => s.StudentId == id).FirstOrDefault();

            return View(std);
        }

        [HttpPost]
        public ActionResult Edit(Student std)
        {
            //write code to update student 
            //update student in DB in real-life application

            //update list by removing old student and adding updated student for demo 
            var student = studentList.Where(s => s.StudentId == std.StudentId).FirstOrDefault();
            studentList.Remove(student);
            studentList.Add(std);
            return RedirectToAction("Index");
        }


        public ActionResult Details(int id)
        {
            //Get the student from studentList sample collection for demo purpose.
            //You can get the student from the database in the real application
            var std = studentList.Where(s => s.StudentId == id).FirstOrDefault();

            return View(std);
        }


        

        public ActionResult Delete(int id)
        {
            //Get the student from studentList sample collection for demo purpose.
            //You can get the student from the database in the real application
            var std = studentList.Where(s => s.StudentId == id).FirstOrDefault();

            return View(std);
        }


        [HttpPost]
        public ActionResult Delete(Student std)
        {
            //write code to update student 
            //update student in DB in real-life application

            //update list by removing old student and adding updated student for demo 
            var student = studentList.Where(s => s.StudentId == std.StudentId).FirstOrDefault();
            studentList.Remove(student);
            return RedirectToAction("Index");
        }

    }


}

.................................View/Details.............................

@model MVCDemo.Models.Student

@{
    ViewBag.Title = "Details";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Details</h2>

<div>
    <h4>Student</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.StudentName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.StudentName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Age)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Age)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.StudentId }) |
    @Html.ActionLink("Back to List", "Index")
</p>

.............................................Views/Delete........................................

@model MVCDemo.Models.Student

@{
    ViewBag.Title = "Delete";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>
<div>
    <h4>Student</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.StudentName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.StudentName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Age)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Age)
        </dd>

    </dl>

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.HiddenFor(model => model.StudentId)

        <div class="form-actions no-color">
            <input type="submit" value="Delete" class="btn btn-default" /> |
            @Html.ActionLink("Back to List", "Index")
        </div>
    }
</div>

....................................................................

No comments:

Post a Comment

Lab 3 Unit -5 Database

   private void button1_Click(object sender, EventArgs e)         {             ProductDbEntities productDb = new ProductDbEntities();      ...