Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Entities/Students.cs
- public class Student
- {
- public int Id { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public int Age { get; set; }
- public string Major { get; set; }
- }
- //ViewModels/Home/IndexVM.cs
- public class IndexVM
- {
- public List<Student> Items { get; set; }
- }
- //Models/StudentsDbContext.cs
- public class StudentsDbContext : DbContext
- {
- public DbSet<Student> Students { get; set; }
- public StudentsDbContext()
- : base(@"Server=PC-PC\SQLEXPRESS;Database=studentsdb;Trusted_Connection=True;")
- {
- this.Students = this.Set<Student>();
- }
- }
- //Controllers/HomeController.cs
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- StudentsDbContext context = new StudentsDbContext();
- IndexVM model = new IndexVM();
- model.Items = context.Students.ToList();
- return View(model);
- }
- [HttpGet]
- public ActionResult Edit(int? id)
- {
- StudentsDbContext context = new StudentsDbContext();
- Student model = null;
- if (id == null)
- {
- model = new Student();
- }
- else
- {
- model = context.Students
- .Where(s => s.Id == id.Value)
- .FirstOrDefault();
- }
- return View(model);
- }
- [HttpPost]
- public ActionResult Edit(Student model)
- {
- StudentsDbContext context = new StudentsDbContext();
- if (model.Age <= 17 && model.Id <= 0 && context.Students
- .Where(s => s.Major == model.Major)
- .Count() >= 10)
- {
- return View(model);
- }
- if (model.Id > 0)
- {
- DbEntityEntry entry = context.Entry(model);
- entry.State = EntityState.Modified;
- }
- else
- {
- context.Students.Add(model);
- }
- context.SaveChanges();
- return RedirectToAction("Index");
- }
- public ActionResult Delete(int id)
- {
- StudentsDbContext context = new StudentsDbContext();
- Student item = context.Students
- .Where(s => s.Id == id)
- .FirstOrDefault();
- context.Students.Remove(item);
- context.SaveChanges();
- return RedirectToAction("Index");
- }
- }
- //Views/Home/Index.cshtml
- @model StudentsManager.ViewModels.Home.IndexVM
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- </head>
- <body>
- <div>
- <a href="/Home/Edit">New</a>
- <table>
- <tr>
- <td>First Name</td>
- <td>Last Name</td>
- <td>Age</td>
- <td>Major</td>
- <td></td>
- </tr>
- @foreach(var item in Model.Items)
- {
- <tr>
- <td>@item.FirstName</td>
- <td>@item.LastName</td>
- <td>@item.Age</td>
- <td>@item.Major</td>
- <td>
- <a href="/Home/Edit?id=@item.Id">edit</a>
- <a href="/Home/Delete?id=@item.Id">delete</a>
- </td>
- </tr>
- }
- </table>
- </div>
- </body>
- </html>
- //Views/Home/Edit.cshtml
- @model StudentsManager.Entities.Student
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Edit</title>
- </head>
- <body>
- <div>
- <form action="/Home/Edit" method="post">
- <input type="hidden" name="Id" value="@Model.Id" />
- First Name:
- <input type="text" name="FirstName" value="@Model.FirstName" />
- <br />
- Last Name:
- <input type="text" name="LastName" value="@Model.LastName" />
- <br />
- Age:
- <input type="text" name="Age" value="@Model.Age" />
- <br />
- Major:
- <input type="text" name="Major" value="@Model.Major" />
- <br />
- <input type="submit" value="Save" />
- <a href="/Home/Index">Back</a>
- </form>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement