Crud without Scaffolded
Packages
"Microsoft.EntityFrameworkCore" Version="5.0.0"
"Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0"
"Microsoft.EntityFrameworkCore.Tools" Version="5.0.0"
"Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2"
Controllers[Folder]
CoursesController.cs
using ManualOperations.Data;
using ManualOperations.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ManualOperations.Controllers
{
public class CoursesController : Controller
{
private readonly DataContext _context;
public CoursesController(DataContext context)
{
_context = context;
}
//Get Section of Course Model
[HttpGet]
public IActionResult Index()
{
var courses = _context.Courses.ToList();
return View();
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpGet]
public IActionResult Detail(int id)
{
var courses = _context.Courses.Where(x => x.Id == id).FirstOrDefault();//Lamda Expression
return View();
}
[HttpGet]
public IActionResult Edit(int id)
{
var courses = _context.Courses.Where(x => x.Id == id).FirstOrDefault();//Lamda Expression
return View();
}
//Post Section of Course Model
[HttpPost]
public IActionResult Create(Course model)
{
return View();
}
}
}
StudentController.cs
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ManualOperations.Controllers
{
public class StudentController : Controller
{
public IActionResult StudentBio()
{
return View();
}
}
}
Data[Folder]
DataContext.cs
using ManualOperations.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ManualOperations.Data
{
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext>options):base(options)
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().ToTable("Student_Table");
modelBuilder.Entity<StudentCourse>().ToTable("StudentCourse_Table");
modelBuilder.Entity<Course>().ToTable("Course_Table");
}
}
}
Models[Folder]
Course.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ManualOperations.Models
{
public class Course
{
public int Id { get; set; }
public string Title { get; set; }
public string Code { get; set; }
public ICollection<StudentCourse> Enrollment { get; set; }
}
}
Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ManualOperations.Models
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Enrolled { get; set; }
public ICollection<StudentCourse> Enrollment { get; set; }
}
}
StudentCourse.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ManualOperations.Models
{
public class StudentCourse
{
public int Id { get; set; }
public int StudentId { get; set; }
public int CourseId { get; set; }
public Student Student { get; set; }
public Course Course { get; set; }
}
}
Views[Folder]
Courses
Create.cshtml
@model ManualOperations.Models.Course
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Course</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Title" class="control-label"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Code" class="control-label"></label>
<input asp-for="Code" class="form-control" />
<span asp-validation-for="Code" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
Detail.cshtml
@model ManualOperations.Models.Course
@{
ViewData["Title"] = "Detail";
}
<h1>Detail</h1>
<div>
<h4>Course</h4>
<hr />
<dl class="row">
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Title)
</dd>
<dt class = "col-sm-2">
@Html.DisplayNameFor(model => model.Code)
</dt>
<dd class = "col-sm-10">
@Html.DisplayFor(model => model.Code)
</dd>
</dl>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model.Id">Edit</a> |
<a asp-action="Index">Back to List</a>
</div>
Edit.cshtml
@model ManualOperations.Models.Course
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>Course</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="Title" class="control-label"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Code" class="control-label"></label>
<input asp-for="Code" class="form-control" />
<span asp-validation-for="Code" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
Index.cshtml
@model IEnumerable<ManualOperations.Models.Course>
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
<th>
@Html.DisplayNameFor(model => model.Code)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.Code)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
Student
StudentBio.cshtml
@{
ViewData["Title"] = "StudentBio";
}
<h1>StudentBio</h1>
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=CHETUIWK0541\\SQL2019NEW;Database=GST;Trusted_Connection=True;MultipleActiveResultSets=True"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Startup.cs
using ManualOperations.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ManualOperations
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<DataContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Student}/{action=Index}/{id?}");
});
}
}
}
0 Comments