Program.cs
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddDbContext<UserDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("CoreCRUDContext") ));
-----
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
---------------------------------------------------------------
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"CoreCRUDContext": "Data Source=ASUS-VIVOBOOK;Initial Catalog=SurajDB;Integrated Security=True;Encrypt=True;Trust Server Certificate=True"
}
}
----------------------------------------------
Dependecies
----------------------------------------------
package manager console:
add-migration initial ---> cmd
update-database
-------------------------------------------------
UserDbContext.cs
using Microsoft.EntityFrameworkCore;
using CoreCRUD.Models;
namespace CoreCRUD.Data
{
public class UserDbContext: DbContext
{
public UserDbContext(DbContextOptions dbContextOptions) :base(dbContextOptions) { }
public DbSet<User> users { get; set; }
}
}
-------------------------------------------------
Usercontroller.cs
using CoreCRUD.Data;
using CoreCRUD.Models;
using Microsoft.AspNetCore.Mvc;
namespace CoreCRUD.Controllers
{
public class UserController: Controller
{
private UserDbContext dbContext;
public UserController(UserDbContext dbContext)
{
this.dbContext = dbContext;
}
public IActionResult Index()
{
var res = dbContext.users.ToList();
return View(res);
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(User user)
{
if (ModelState.IsValid)
{
var u = new User()
{
Name = user.Name,
Email = user.Email
};
dbContext.users.Add(u);
dbContext.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
public IActionResult Edit(int id)
{
User res = dbContext.users.SingleOrDefault(u => u.Id == id);
return View(res);
}
[HttpPost]
public IActionResult Edit(User user)
{
dbContext.users.Update(user);
dbContext.SaveChanges();
return RedirectToAction("Index");
}
public IActionResult Delete(int id)
{
User res = dbContext.users.SingleOrDefault(u => u.Id == id);
dbContext.users.Remove(res);
dbContext.SaveChanges();
return RedirectToAction("Index");
}
}
}
_------------------------------------------
User.cs Model
using System.ComponentModel.DataAnnotations;
namespace CoreCRUD.Models
{
public class User
{
[Required]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Email { get; set; }
}
}
-------------------------------------------------------
Index.cshtml
@model IEnumerable<CoreCRUD.Models.User>
@{
ViewData["Title"] = "Indez";
}
<style>
*{
padding:5px;
}
</style>
<div>
@Html.ActionLink("Create", "Create", "User")
</div>
<div>
<table>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>
@foreach(var s in Model)
{
<tr>
<td>@s.Id</td>
<td>@s.Name</td>
<td>@s.Email</td>
<td>
@Html.ActionLink("Edit","Edit","User",new { id = s.Id})
@Html.ActionLink("Delete","Delete","User", new{ id = s.Id})
</td>
</tr>
}
</table>
</div>
-------------------------------------------------
Edit.cshtml
@{
ViewData["Title"] = "Edit";
}
@model CoreCRUD.Models.User;
<style>
* {
padding: 5px;
}
</style>
<div>
<form method="post" asp-action="Edit" >
<div>
<label asp-for = "Name">Name:</label>
<input asp-for="Name" type="text" value="@Model.Name" name="Name" required/>
</div>
<div>
<label asp-for="Email">Email :</label>
<input asp-for="Email" type="text" value="@Model.Email" name="Email" required />
</div>
<div>
@Html.ActionLink("Cancel","Index","User")
<input type="submit" value="Update" class="btn btn-success"/>
</div>
</form>
</div>
-----------------------------------------------
Create.cshtml
@model CoreCRUD.Models.User
@{
ViewData["Title"] = "Create";
}
<style>
* {
padding: 5px;
}
</style>
<div>
<form method="post" asp-action="create">
<div>
<label asp-for="Name">Name: </label>
<input asp-for="Name" name="Name" type="text" placeholder="Enter Name Here"/>
</div>
<div>
<label asp-for="Email">Email: </label>
<input asp-for="Email" name="Email" type="text" placeholder="Enter Email Here" />
</div>
<div>
@Html.ActionLink("Cancel", "Index", "User")
<input type="submit" value="Save" class="btn btn-primary"/>
</div>
</form>
</div>
0 Comments