Advertisement
Guest User

.NET MVC v2

a guest
Mar 25th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.68 KB | None | 0 0
  1. //Entities/Students.cs
  2. public class Student
  3.     {
  4.         public int Id { get; set; }
  5.         public string FirstName { get; set; }
  6.         public string LastName { get; set; }
  7.         public int Age { get; set; }
  8.         public string Major { get; set; }
  9.     }
  10.  
  11. //ViewModels/Home/IndexVM.cs
  12. public class IndexVM
  13.     {
  14.         public List<Student> Items { get; set; }
  15.     }
  16.  
  17. //Models/StudentsDbContext.cs
  18. public class StudentsDbContext : DbContext
  19.     {
  20.         public DbSet<Student> Students { get; set; }
  21.  
  22.         public StudentsDbContext()
  23.     : base(@"Server=PC-PC\SQLEXPRESS;Database=studentsdb;Trusted_Connection=True;")
  24.         {
  25.             this.Students = this.Set<Student>();
  26.         }
  27.     }
  28.  
  29. //Controllers/HomeController.cs
  30. public class HomeController : Controller
  31.     {
  32.         public ActionResult Index()
  33.         {
  34.             StudentsDbContext context = new StudentsDbContext();
  35.  
  36.             IndexVM model = new IndexVM();
  37.             model.Items = context.Students.ToList();
  38.            
  39.             return View(model);
  40.         }
  41.  
  42.         [HttpGet]
  43.         public ActionResult Edit(int? id)
  44.         {
  45.             StudentsDbContext context = new StudentsDbContext();
  46.  
  47.             Student model = null;
  48.  
  49.             if (id == null)
  50.             {
  51.                 model = new Student();
  52.             }
  53.             else
  54.             {
  55.                 model = context.Students
  56.                                .Where(s => s.Id == id.Value)
  57.                                .FirstOrDefault();
  58.             }
  59.  
  60.             return View(model);
  61.         }
  62.  
  63.         [HttpPost]
  64.         public ActionResult Edit(Student model)
  65.         {
  66.             StudentsDbContext context = new StudentsDbContext();
  67.  
  68.             if (model.Age <= 17 && model.Id <= 0 && context.Students
  69.                                         .Where(s => s.Major == model.Major)
  70.                                         .Count() >= 10)
  71.             {
  72.                 return View(model);
  73.             }
  74.  
  75.             if (model.Id > 0)
  76.             {
  77.                 DbEntityEntry entry = context.Entry(model);
  78.                 entry.State = EntityState.Modified;
  79.             }
  80.             else
  81.             {
  82.                 context.Students.Add(model);
  83.             }
  84.  
  85.             context.SaveChanges();
  86.  
  87.             return RedirectToAction("Index");
  88.         }
  89.  
  90.         public ActionResult Delete(int id)
  91.         {
  92.             StudentsDbContext context = new StudentsDbContext();
  93.  
  94.             Student item = context.Students
  95.                                       .Where(s => s.Id == id)
  96.                                       .FirstOrDefault();
  97.  
  98.             context.Students.Remove(item);
  99.             context.SaveChanges();
  100.  
  101.             return RedirectToAction("Index");
  102.         }
  103.     }
  104.  
  105. //Views/Home/Index.cshtml
  106. @model StudentsManager.ViewModels.Home.IndexVM
  107.  
  108. @{
  109.     Layout = null;
  110. }
  111.  
  112. <!DOCTYPE html>
  113.  
  114. <html>
  115. <head>
  116.     <meta name="viewport" content="width=device-width" />
  117.     <title>Index</title>
  118. </head>
  119. <body>
  120.     <div>
  121.         <a href="/Home/Edit">New</a>
  122.         <table>
  123.             <tr>
  124.                 <td>First Name</td>
  125.                 <td>Last Name</td>
  126.                 <td>Age</td>
  127.                 <td>Major</td>
  128.                 <td></td>
  129.             </tr>
  130.         @foreach(var item in Model.Items)
  131.         {
  132.             <tr>
  133.                 <td>@item.FirstName</td>
  134.                 <td>@item.LastName</td>
  135.                 <td>@item.Age</td>
  136.                 <td>@item.Major</td>
  137.                 <td>
  138.                     <a href="/Home/Edit?id=@item.Id">edit</a>
  139.                     <a href="/Home/Delete?id=@item.Id">delete</a>
  140.                 </td>
  141.             </tr>
  142.         }
  143.         </table>
  144.     </div>
  145. </body>
  146. </html>
  147.  
  148. //Views/Home/Edit.cshtml
  149. @model StudentsManager.Entities.Student
  150.  
  151. @{
  152.     Layout = null;
  153. }
  154.  
  155. <!DOCTYPE html>
  156.  
  157. <html>
  158. <head>
  159.     <meta name="viewport" content="width=device-width" />
  160.     <title>Edit</title>
  161. </head>
  162. <body>
  163.     <div>
  164.         <form action="/Home/Edit" method="post">
  165.             <input type="hidden" name="Id" value="@Model.Id" />
  166.  
  167.             First Name:
  168.             <input type="text" name="FirstName" value="@Model.FirstName" />
  169.             <br />
  170.             Last Name:
  171.             <input type="text" name="LastName" value="@Model.LastName" />
  172.             <br />
  173.             Age:
  174.             <input type="text" name="Age" value="@Model.Age" />
  175.             <br />
  176.             Major:
  177.             <input type="text" name="Major" value="@Model.Major" />
  178.             <br />
  179.             <input type="submit" value="Save" />
  180.             <a href="/Home/Index">Back</a>
  181.         </form>
  182.     </div>
  183. </body>
  184. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement