Sunday, May 7, 2023

Unit-4 Lab



 

 

 

Models/Student.cs

 

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; }

    }

}

 

Controllers / StudentController.cs

 

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");

        }

 

    }

 

 

}

 

 

Index.cshtml

@model IEnumerable<MVCDemo.Models.Student>

 

@{

    ViewBag.Title = "Index";

    Layout = "~/Views/Shared/_Layout.cshtml";

}

 

<h2>Index</h2>

 

<p>

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

</p>

<table class="table">

    <tr>

        <th>

            @Html.DisplayNameFor(model => model.StudentName)

        </th>

        <th>

            @Html.DisplayNameFor(model => model.Age)

        </th>

        <th></th>

    </tr>

 

    @foreach (var item in Model)

    {

        <tr>

            <td>

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

            </td>

            <td>

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

            </td>

            <td>

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

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

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

            </td>

        </tr>

    }

 

</table>

 

Edit.cshtml

@model MVCDemo.Models.Student

 

@{

    ViewBag.Title = "Edit";

    Layout = "~/Views/Shared/_Layout.cshtml";

}

 

<h2>Edit</h2>

 

@using (Html.BeginForm())

{

    @Html.AntiForgeryToken()

 

    <div class="form-horizontal">

        <h4>Student</h4>

        <hr />

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @Html.HiddenFor(model => model.StudentId)

 

        <div class="form-group">

            @Html.LabelFor(model => model.StudentName, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.StudentName, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.StudentName, "", new { @class = "text-danger" })

            </div>

        </div>

 

        <div class="form-group">

            @Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })

            <div class="col-md-10">

                @Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })

                @Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })

            </div>

        </div>

 

        <div class="form-group">

            <div class="col-md-offset-2 col-md-10">

                <input type="submit" value="Save" class="btn btn-default" />

            </div>

        </div>

    </div>

}

 

<div>

    @Html.ActionLink("Back to List", "Index")

</div>

 

 

https://www.tutorialsteacher.com/mvc/create-edit-view-in-asp.net-mvc

https://www.codingninjas.com/codestudio/library/asp-net-create-edit-view

http://www.dotnettpoint.com/MVC/CreateEditView

https://stackoverflow.com/questions/42202510/need-to-display-the-required-field-of-the-student-table

 

No comments:

Post a Comment

Lab 3 Unit -5 Database

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