using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace BootstarpModalPopup.Models
{
public class DEPT
{
[Key]
public int DID { get; set; }
[MaxLength(50)]
public string DNAME { get; set; }
[MaxLength(50)]
public string GENDER { get; set; }
}
}
--
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace BootstarpModalPopup.Models
{
public class AppDbContext:DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options):base(options)
{
}
public DbSet<DEPT> Depts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=SivDB008;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
}
}
}
--
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace BootstarpModalPopup.Models
{
public class DEPTVM
{
public int DID { get; set; }
[Required(ErrorMessage ="Name should not be blank.")]
public string DNAME { get; set; }
[Required(ErrorMessage = "Please select a Gender")]
public string GENDER { get; set; }
}
}
--
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BootstarpModalPopup.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
namespace BootstarpModalPopup
{
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<AppDbContext>();
services.AddScoped<IRepository<DEPT>, Repository<DEPT>>();
}
// 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");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
--
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BootstarpModalPopup.Models
{
public interface IRepository<T> where T:class
{
Task<IEnumerable<T>> Gets();
Task<T> Get(int ID);
Task<T> Save(T t);
Task<T> Update(T t);
Task<T> Delete(int ID);
Task<T> SaveAll(List<T> lst);
Task<T> DeleteAll(List<T> lst);
}
}
--
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace BootstarpModalPopup.Models
{
public class Repository<T> : IRepository<T> where T : class
{
#region Declaretion
private readonly AppDbContext Context;
#endregion
public Repository(AppDbContext Context)
{
this.Context = Context;
}
public async Task<T> Delete(int ID)
{
T t =await Context.Set<T>().FindAsync(ID);
Context.Set<T>().Remove(t);
await Context.SaveChangesAsync();
return t;
}
public async Task<T> DeleteAll(List<T> lst)
{
Context.Set<T>().RemoveRange(lst);
await Context.SaveChangesAsync();
return lst.FirstOrDefault();
}
public async Task<T> Get(int ID)
{
return await Context.Set<T>().FindAsync(ID);
}
public async Task<IEnumerable<T>> Gets()
{
return await Context.Set<T>().ToListAsync();
}
public async Task<T> Save(T t)
{
await Context.Set<T>().AddAsync(t);
await Context.SaveChangesAsync();
return t;
}
public async Task<T> SaveAll(List<T> lst)
{
await Context.Set<T>().AddRangeAsync(lst);
await Context.SaveChangesAsync();
return lst.FirstOrDefault();
}
public async Task<T> Update(T t)
{
Context.Set<T>().Update(t);
await Context.SaveChangesAsync();
return t;
}
}
}
--
Controller :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BootstarpModalPopup.Models;
namespace BootstarpModalPopup.Controllers
{
public class DeptController : Controller
{
private readonly IRepository<DEPT> Repository;
public DeptController(IRepository<DEPT> Repository)
{
this.Repository = Repository;
}
[HttpGet]
public async Task<IActionResult> Index()
{
var data = await Repository.Gets();
return View(data);
}
[HttpGet]
public async Task<IActionResult> OPenpopup(int id)
{
DEPTVM dEPTVM = new DEPTVM();
dEPTVM.DID = id;
DEPT dept = await Repository.Get(id);
if (id==0)
{
return PartialView("AddorEditPV", dEPTVM);
}
dEPTVM.DNAME = dept.DNAME;
dEPTVM.GENDER = dept.GENDER;
return PartialView("AddorEditPV", dEPTVM);
}
public async Task<IActionResult> Delete(int id)
{
DEPT dept = await Repository.Delete(id);
return RedirectToAction("Index");
}
[HttpPost]
public async Task<IActionResult> AddorEdit(DEPTVM dEPTVM)
{
DEPT dept = new DEPT();
dept.DNAME = dEPTVM.DNAME;
dept.GENDER = dEPTVM.GENDER;
if (dEPTVM.DID==0)
{
dept = await Repository.Save(dept);
}
else
{
dept.DID = dEPTVM.DID;
dept = await Repository.Update(dept);
}
return RedirectToAction("Index");
}
}
}
--
Index View :
@model IEnumerable<BootstarpModalPopup.Models.DEPT>
@{
ViewData["Title"] = "Index";
}
<style type="text/css">
.xxx {
background-color: #0069d9;
border-color: #0062cc;
color: white
}
.modal-header {
display: block !important;
}
.modal-title {
float: left;
}
.modal-header .close {
float: right;
}
</style>
<p>
<a class="btn btn-primary xxx" id="btnAdd" href="#">Add New</a>
</p>
<table class="table table-bordered table-hover">
<thead class="xxx">
<tr>
<th>
@Html.DisplayNameFor(model => model.DID)
</th>
<th>
@Html.DisplayNameFor(model => model.DNAME)
</th>
<th>
@Html.DisplayNameFor(model => model.GENDER)
</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.DID)
</td>
<td>
@Html.DisplayFor(modelItem => item.DNAME)
</td>
<td>
@Html.DisplayFor(modelItem => item.GENDER)
</td>
<td>
<a href="#" class="edit">Edit</a> | <a href="#" onclick="confirmDelete('@Url.Content("~/Dept/Delete/" + item.DID)')">Delete</a>
</td>
</tr>
}
</tbody>
</table>
<partial name="ConfirmPV" />
@*<partial name="AddorEditPV" model='new DEPTVM() { }' />*@
<div id="dv"></div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script type="text/javascript">
$(function () {
var modalContainer = $('#dv');
$('#btnAdd').click(function () {
fx(0);
});
$('.table').on("click",".edit", function (e) {
var id = $(this).closest("tr").find("td:first").text();
e.stopPropagation();
fx(id);
});
function fx(Id) {
$.ajax({
"url": '@Url.Action("OpenPopup", "Dept")',
type: 'Get',
datatype: 'json',
cache: false,
async: true,
data: {id:Id},
success: function (data) {
modalContainer.empty().append(data);
modalContainer.find("[id='deleteModal1']").modal('show');
},
error: function (t) {
alert(t.responseText);
}
});
}
});
</script>
}
@model IEnumerable<BootstarpModalPopup.Models.DEPT>
@{
ViewData["Title"] = "Index";
}
<style type="text/css">
.xxx {
background-color: #0069d9;
border-color: #0062cc;
color: white
}
.modal-header {
display: block !important;
}
.modal-title {
float: left;
}
.modal-header .close {
float: right;
}
</style>
<p>
<a class="btn btn-primary xxx" id="btnAdd" href="#">Add New</a>
</p>
<table class="table table-bordered table-hover">
<thead class="xxx">
<tr>
<th>
@Html.DisplayNameFor(model => model.DID)
</th>
<th>
@Html.DisplayNameFor(model => model.DNAME)
</th>
<th>
@Html.DisplayNameFor(model => model.GENDER)
</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.DID)
</td>
<td>
@Html.DisplayFor(modelItem => item.DNAME)
</td>
<td>
@Html.DisplayFor(modelItem => item.GENDER)
</td>
<td>
<a href="#" class="edit">Edit</a> | <a href="#" onclick="confirmDelete('@Url.Content("~/Dept/Delete/" + item.DID)')">Delete</a>
</td>
</tr>
}
</tbody>
</table>
<partial name="ConfirmPV" />
@*<partial name="AddorEditPV" model='new DEPTVM() { }' />*@
<div id="dv"></div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script type="text/javascript">
$(function () {
var modalContainer = $('#dv');
$('#btnAdd').click(function () {
fx(0);
});
$('.table').on("click",".edit", function (e) {
var id = $(this).closest("tr").find("td:first").text();
e.stopPropagation();
fx(id);
});
function fx(Id) {
$.ajax({
"url": '@Url.Action("OpenPopup", "Dept")',
type: 'Get',
datatype: 'json',
cache: false,
async: true,
data: {id:Id},
success: function (data) {
modalContainer.empty().append(data);
modalContainer.find("[id='deleteModal1']").modal('show');
},
error: function (t) {
alert(t.responseText);
}
});
}
});
</script>
}
Add or Edit Partial View :
@model DEPTVM
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.min.js"></script>
<form asp-action="AddorEdit">
<div class="modal fade" id="deleteModal1" data-keyboard="false" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Add Or Edit</h4>
</div>
<div class="modal-body">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="DNAME" class="control-label"></label>
<input asp-for="DNAME" class="form-control" />
<input asp-for="DID" type="hidden" />
<span asp-validation-for="DNAME" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="GENDER" class="control-label"></label>
<input type="radio" asp-for="GENDER" value="Male" />Male
<input type="radio" asp-for="GENDER" value="Female" />Female
<span asp-validation-for="GENDER" class="text-danger"></span>
</div>
</div>
<div class="modal-footer">
<input type="submit" id="deleteConfirm" value="Create" class="btn btn-primary" style="width:80px" />
<input type="button" value="Cancel" class="btn btn-primary focusedButton" data-dismiss="modal" style="width:80px" />
</div>
</div>
</div>
</div>
</form>
@model DEPTVM
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.min.js"></script>
<form asp-action="AddorEdit">
<div class="modal fade" id="deleteModal1" data-keyboard="false" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Add Or Edit</h4>
</div>
<div class="modal-body">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="DNAME" class="control-label"></label>
<input asp-for="DNAME" class="form-control" />
<input asp-for="DID" type="hidden" />
<span asp-validation-for="DNAME" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="GENDER" class="control-label"></label>
<input type="radio" asp-for="GENDER" value="Male" />Male
<input type="radio" asp-for="GENDER" value="Female" />Female
<span asp-validation-for="GENDER" class="text-danger"></span>
</div>
</div>
<div class="modal-footer">
<input type="submit" id="deleteConfirm" value="Create" class="btn btn-primary" style="width:80px" />
<input type="button" value="Cancel" class="btn btn-primary focusedButton" data-dismiss="modal" style="width:80px" />
</div>
</div>
</div>
</div>
</form>
This partial view keep in the share folder .
Confirm partial view :
<div class="modal fade" id="deleteModal" data-keyboard="false" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Confirmation Message</h4>
</div>
<div class="modal-body">
Do you want to delete it. ?
</div>
<div class="modal-footer">
<a id="deleteConfirm" href="#" class="btn btn-primary" style="width:80px">Yes</a>
<button type="button" class="btn btn-primary focusedButton" data-dismiss="modal" style="width:80px">No</button>
</div>
</div>
</div>
</div>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.min.js"></script>
<script>
var x= $('#deleteModal');
function confirmDelete(url) {
$("#deleteConfirm").attr("href", url);
x.modal('show')
}
$('body').keypress(function (e) {
if (e.which === 13) {
$(".focusedButton").trigger('click');
}
});
</script>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Confirmation Message</h4>
</div>
<div class="modal-body">
Do you want to delete it. ?
</div>
<div class="modal-footer">
<a id="deleteConfirm" href="#" class="btn btn-primary" style="width:80px">Yes</a>
<button type="button" class="btn btn-primary focusedButton" data-dismiss="modal" style="width:80px">No</button>
</div>
</div>
</div>
</div>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.min.js"></script>
<script>
var x= $('#deleteModal');
function confirmDelete(url) {
$("#deleteConfirm").attr("href", url);
x.modal('show')
}
$('body').keypress(function (e) {
if (e.which === 13) {
$(".focusedButton").trigger('click');
}
});
</script>
No comments:
Post a Comment