First install some nugget package for EDMX.
Tools > NuGet Package Manager -> Package Manager Console
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design
after install above
Go to Tools –> NuGet Package Manager –> Package Manager Console
And then run the following command to create a model from the existing database:
Scaffold-DbContext "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Sankar;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" Microsoft.EntityFrameworkCore.SqlServer -OutputDir DAL
View model code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Web;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations.Schema;
namespace WebApplication1.ViewModel
{
public class EMPVM
{
[Required(ErrorMessage ="Eid should not be blank.")]
public int? Eid { get; set; }
[Required(ErrorMessage ="Name should not be blank.")]
public string Name { get; set; }
[DataType(DataType.MultilineText)]
[Required(ErrorMessage ="Address should not be blank.")]
public string Address { get; set; }
[DataType(DataType.Password)]
[Required(ErrorMessage ="Password should not be blank.")]
[StringLength(8,MinimumLength =6, ErrorMessage ="Password length between 6 and 8 charecters long.")]
[Remote("Check","Emp",ErrorMessage = "This password already exists.")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Required(ErrorMessage = "Confirm assword should not be blank.")]
[Compare("Password",ErrorMessage ="Password and confrim password must be same.")]
[DisplayName("Confirm Password")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage ="Please select a gender.")]
public string Gender { get; set; }
[Required(ErrorMessage = "Email should not be blank.")]
[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage ="Invalid email id.")]
public string Email { get; set; }
[Required(ErrorMessage = "Salary should not be blank.")]
[Range(1000,500000,ErrorMessage ="Salary between 10000 and 500000.")]
public decimal? Salary { get; set; }
[Required(ErrorMessage = "DOB should not be blank.")]
public DateTime? Dob { get; set; }
[Required(ErrorMessage = "Please select a country.")]
public int? Country { get; set; }
public List<SelectListItem> Countrylist { get; set; }
[Required(ErrorMessage = "Please select a state.")]
public int? State { get; set; }
public List<SelectListItem> Statelist { get; set; }
[Customvalidation(ErrorMessage ="Please select a hobby.")]
public List<int> Hobby { get; set; }
public List<SelectListItem> Hobbylist { get; set; }
public EMPVM()
{
Countrylist = new List<SelectListItem>();
Statelist = new List<SelectListItem>();
Hobbylist = new List<SelectListItem>();
Hobby = new List<int>();
}
}
public class Customvalidation:ValidationAttribute
{
public override bool IsValid(object value)
{
return ((List<int>)value).Count > 0;
}
}
}
Controller Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.EntityFrameworkCore;
using WebApplication1.DAL;
using WebApplication1.ViewModel;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;
namespace WebApplication1.Controllers
{
public class EmpController : Controller
{
public JsonResult Check(string Password)
{
using (SankarContext obj = new SankarContext())
{
return Json(!obj.Emp.Any(e => e.Password == Password));
}
}
[HttpGet]
public JsonResult fillddl(int CID)
{
using (SankarContext obj = new SankarContext())
{
return Json(JsonConvert.SerializeObject(obj.State.Where(e => e.Cid == CID).ToList()));
}
}
[HttpGet]
public async Task<IActionResult> Index()
{
using (SankarContext obj = new SankarContext())
{
var data = await obj.Emp.ToListAsync();
if (data == null)
return NoContent();
return View(data);
}
}
[HttpGet]
public IActionResult Create()
{
using (SankarContext obj = new SankarContext())
{
EMPVM vm = new EMPVM();
vm.Countrylist = obj.Country.Select(e => new SelectListItem { Value = e.Cid.ToString(), Text = e.Cname }).ToList();
vm.Statelist = new List<SelectListItem>();
vm.Hobbylist = obj.Hobby.Select(e => new SelectListItem { Value = e.Hid.ToString(), Text = e.Hname }).ToList();
return View(vm);
}
}
[HttpPost]
public async Task<IActionResult> Create(EMPVM vm)
{
using (SankarContext obj = new SankarContext())
{
if (ModelState.IsValid)
{
Emp emp = new Emp();
emp.Eid = vm.Eid ?? 0;
emp.Name = vm.Name;
emp.Address = vm.Address;
emp.Password = vm.Password;
emp.Gender = vm.Gender;
emp.Email = vm.Email;
emp.Salary = vm.Salary;
emp.Dob = vm.Dob;
emp.Cid = vm.Country;
emp.Sid = vm.State;
await obj.Emp.AddAsync(emp);
await obj.SaveChangesAsync();
await obj.Emphobbymap.AddRangeAsync(vm.Hobby.Select(e => new Emphobbymap { Eid = vm.Eid, Hid = e }).ToList());
await obj.SaveChangesAsync();
return RedirectToAction("Index");
}
else
{
vm.Countrylist = obj.Country.ToList().Select(e => new SelectListItem { Value = e.Cid.ToString(), Text = e.Cname }).ToList();
vm.Statelist = obj.State.Where(m => m.Cid == vm.Country.Value).ToList().Select(e => new SelectListItem { Value = e.Sid.ToString(), Text = e.Sname }).ToList();
vm.Hobbylist = obj.Hobby.ToList().Select(e => new SelectListItem { Value = e.Hid.ToString(), Text = e.Hname }).ToList();
return View(vm);
}
}
}
[HttpGet]
public async Task<IActionResult> Edit(int? id)
{
using (SankarContext obj = new SankarContext())
{
if (id == null)
return NoContent();
Emp emp = await obj.Emp.SingleOrDefaultAsync(e => e.Eid == id);
EMPVM vm = new EMPVM();
vm.Eid = emp.Eid;
vm.Name = emp.Name;
vm.Address = emp.Address;
vm.Gender = emp.Gender;
vm.Email = emp.Email;
vm.Salary = emp.Salary;
vm.Dob = emp.Dob;
vm.Country = emp.Cid;
vm.State = emp.Sid;
vm.Countrylist = obj.Country.Select(e => new SelectListItem { Value = e.Cid.ToString(), Text = e.Cname }).ToList();
vm.Statelist = obj.State.Where(e => e.Cid == emp.Cid).Select(e => new SelectListItem { Value = e.Sid.ToString(), Text = e.Sname }).ToList();
vm.Hobbylist = obj.Hobby.Select(e => new SelectListItem { Value = e.Hid.ToString(), Text = e.Hname }).ToList();
vm.Hobby = obj.Emphobbymap.Where(e => e.Eid == id).Select(m => m.Hid.Value).ToList();
return View(vm);
}
}
[HttpPost]
public async Task<IActionResult> Edit(EMPVM vm)
{
using (SankarContext obj = new SankarContext())
{
if (vm == null)
return NoContent();
ModelState.Remove("Password");
ModelState.Remove("ConfirmPassword");
if (ModelState.IsValid)
{
Emp emp = await obj.Emp.SingleOrDefaultAsync(e => e.Eid == vm.Eid);
emp.Name = vm.Name;
emp.Address = vm.Address;
emp.Password = vm.Password;
emp.Gender = vm.Gender;
emp.Email = vm.Email;
emp.Salary = vm.Salary;
emp.Dob = vm.Dob;
emp.Cid = vm.Country;
emp.Sid = vm.State;
obj.Emp.Update(emp);
await obj.SaveChangesAsync();
obj.Emphobbymap.RemoveRange(obj.Emphobbymap.Where(e => e.Eid == vm.Eid));
await obj.SaveChangesAsync();
await obj.Emphobbymap.AddRangeAsync(vm.Hobby.Select(e => new Emphobbymap { Eid = vm.Eid, Hid = e }).ToList());
await obj.SaveChangesAsync();
return RedirectToAction("Index");
}
else
{
vm.Countrylist = obj.Country.ToList().Select(e => new SelectListItem { Value = e.Cid.ToString(), Text = e.Cname }).ToList();
vm.Statelist = obj.State.Where(m => m.Cid == vm.Country.Value).ToList().Select(e => new SelectListItem { Value = e.Sid.ToString(), Text = e.Sname }).ToList();
vm.Hobbylist = obj.Hobby.ToList().Select(e => new SelectListItem { Value = e.Hid.ToString(), Text = e.Hname }).ToList();
return View(vm);
}
}
}
[HttpGet]
public async Task<IActionResult> Delete(int? id)
{
using (SankarContext obj = new SankarContext())
{
if (id == null)
return NoContent();
Emp emp = await obj.Emp.SingleOrDefaultAsync(e => e.Eid == id);
obj.Emp.Remove(emp);
await obj.SaveChangesAsync();
obj.Emphobbymap.RemoveRange(obj.Emphobbymap.Where(e => e.Eid == id));
await obj.SaveChangesAsync();
return RedirectToAction("Index");
}
}
}
}
Index view code :
@model IEnumerable<WebApplication1.DAL.Emp>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Salary)
</th>
<th>
@Html.DisplayNameFor(model => model.Dob)
</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
@Html.DisplayFor(modelItem => item.Dob)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Eid">Edit</a> |
<a asp-action="Delete" asp-route-id="@item.Eid">Delete</a>
</td>
</tr>
}
</tbody>
</table>
Create view code :
@model WebApplication1.ViewModel.EMPVM
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<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="Eid" class="control-label"></label>
<input asp-for="Eid" class="form-control" />
<span asp-validation-for="Eid" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Address" class="control-label"></label>
<textarea asp-for="Address" class="form-control"></textarea>
<span asp-validation-for="Address" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password" class="control-label"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ConfirmPassword" class="control-label"></label>
<input asp-for="ConfirmPassword" class="form-control" />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Gender" class="control-label"></label>
<input type="radio" value="Male" asp-for="Gender" />Male
<input type="radio" value="Female" asp-for="Gender" />Female
<span asp-validation-for="Gender" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Salary" class="control-label"></label>
<input asp-for="Salary" class="form-control" />
<span asp-validation-for="Salary" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Dob" class="control-label"></label>
<input type="text" asp-for="Dob" class="form-control" id="dob" />
<span asp-validation-for="Dob" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Country" class="control-label"></label>
<select asp-for="Country" id="ddlc" asp-items="Model.Countrylist" class="form-control">
<option value="">Select</option>
</select>
<span asp-validation-for="Country" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="State" class="control-label"></label>
<select asp-for="State" id="ddls" asp-items="Model.Statelist" class="form-control">
<option value="">Select</option>
</select>
<span asp-validation-for="State" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Hobby" class="control-label"></label>
<select asp-for="Hobby" multiple id="hb" asp-items="Model.Hobbylist" class="form-control"></select>
<span asp-validation-for="Hobby" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
<script type="text/javascript">
$(function () {
$('#ddlc').change(function () {
$.ajax({
url: '@Url.Action("fillddl", "Emp")',
data: { CID: $('#ddlc').val() },
type: 'GET',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (result) {
$('#ddls').empty();
$('#ddls').append("<option value=''>Select</option>");
$.each(JSON.parse(result), function (i, j) {
$('#ddls').append("<option value='" + j.Sid + "'>" + j.Sname + "</option>");
});
}
});
});
});
</script>
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Edit view code :
@model WebApplication1.ViewModel.EMPVM
@{
ViewData["Title"] = "Edit";
}
<h2>Edit</h2>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Eid" class="control-label"></label>
<input asp-for="Eid" class="form-control" />
<span asp-validation-for="Eid" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Address" class="control-label"></label>
<textarea asp-for="Address" class="form-control"></textarea>
<span asp-validation-for="Address" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Gender" class="control-label"></label>
<input type="radio" value="Male" asp-for="Gender" />Male
<input type="radio" value="Female" asp-for="Gender" />Female
<span asp-validation-for="Gender" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Salary" class="control-label"></label>
<input asp-for="Salary" class="form-control" />
<span asp-validation-for="Salary" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Dob" class="control-label"></label>
<input type="text" asp-for="Dob" class="form-control" id="dob" />
<span asp-validation-for="Dob" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Country" class="control-label"></label>
<select asp-for="Country" id="ddlc" asp-items="Model.Countrylist" class="form-control">
<option value="">Select</option>
</select>
<span asp-validation-for="Country" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="State" class="control-label"></label>
<select asp-for="State" id="ddls" asp-items="Model.Statelist" class="form-control">
<option value="">Select</option>
</select>
<span asp-validation-for="State" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Hobby" class="control-label"></label>
<select asp-for="Hobby" multiple id="hb" asp-items="Model.Hobbylist" class="form-control"></select>
<span asp-validation-for="Hobby" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-default" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
<script type="text/javascript">
$(function () {
$('#ddlc').change(function () {
$.getJSON('@Url.Action("fillddl", "Emp")', { CID: $('#ddlc').val() }, function (data1) {
$('#ddls').empty();
$('#ddls').append("<option value=''>Select</option>");
debugger;
$.each(JSON.parse(data1), function (i, j) {
$('#ddls').append("<option value='" + j.Sid + "'>" + j.Sname + "</option>");
});
});
});
});
</script>
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Tools > NuGet Package Manager -> Package Manager Console
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design
after install above
Go to Tools –> NuGet Package Manager –> Package Manager Console
And then run the following command to create a model from the existing database:
Scaffold-DbContext "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Sankar;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" Microsoft.EntityFrameworkCore.SqlServer -OutputDir DAL
View model code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Web;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations.Schema;
namespace WebApplication1.ViewModel
{
public class EMPVM
{
[Required(ErrorMessage ="Eid should not be blank.")]
public int? Eid { get; set; }
[Required(ErrorMessage ="Name should not be blank.")]
public string Name { get; set; }
[DataType(DataType.MultilineText)]
[Required(ErrorMessage ="Address should not be blank.")]
public string Address { get; set; }
[DataType(DataType.Password)]
[Required(ErrorMessage ="Password should not be blank.")]
[StringLength(8,MinimumLength =6, ErrorMessage ="Password length between 6 and 8 charecters long.")]
[Remote("Check","Emp",ErrorMessage = "This password already exists.")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Required(ErrorMessage = "Confirm assword should not be blank.")]
[Compare("Password",ErrorMessage ="Password and confrim password must be same.")]
[DisplayName("Confirm Password")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage ="Please select a gender.")]
public string Gender { get; set; }
[Required(ErrorMessage = "Email should not be blank.")]
[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage ="Invalid email id.")]
public string Email { get; set; }
[Required(ErrorMessage = "Salary should not be blank.")]
[Range(1000,500000,ErrorMessage ="Salary between 10000 and 500000.")]
public decimal? Salary { get; set; }
[Required(ErrorMessage = "DOB should not be blank.")]
public DateTime? Dob { get; set; }
[Required(ErrorMessage = "Please select a country.")]
public int? Country { get; set; }
public List<SelectListItem> Countrylist { get; set; }
[Required(ErrorMessage = "Please select a state.")]
public int? State { get; set; }
public List<SelectListItem> Statelist { get; set; }
[Customvalidation(ErrorMessage ="Please select a hobby.")]
public List<int> Hobby { get; set; }
public List<SelectListItem> Hobbylist { get; set; }
public EMPVM()
{
Countrylist = new List<SelectListItem>();
Statelist = new List<SelectListItem>();
Hobbylist = new List<SelectListItem>();
Hobby = new List<int>();
}
}
public class Customvalidation:ValidationAttribute
{
public override bool IsValid(object value)
{
return ((List<int>)value).Count > 0;
}
}
}
Controller Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.EntityFrameworkCore;
using WebApplication1.DAL;
using WebApplication1.ViewModel;
using Microsoft.AspNetCore.Mvc.Rendering;
using Newtonsoft.Json;
namespace WebApplication1.Controllers
{
public class EmpController : Controller
{
public JsonResult Check(string Password)
{
using (SankarContext obj = new SankarContext())
{
return Json(!obj.Emp.Any(e => e.Password == Password));
}
}
[HttpGet]
public JsonResult fillddl(int CID)
{
using (SankarContext obj = new SankarContext())
{
return Json(JsonConvert.SerializeObject(obj.State.Where(e => e.Cid == CID).ToList()));
}
}
[HttpGet]
public async Task<IActionResult> Index()
{
using (SankarContext obj = new SankarContext())
{
var data = await obj.Emp.ToListAsync();
if (data == null)
return NoContent();
return View(data);
}
}
[HttpGet]
public IActionResult Create()
{
using (SankarContext obj = new SankarContext())
{
EMPVM vm = new EMPVM();
vm.Countrylist = obj.Country.Select(e => new SelectListItem { Value = e.Cid.ToString(), Text = e.Cname }).ToList();
vm.Statelist = new List<SelectListItem>();
vm.Hobbylist = obj.Hobby.Select(e => new SelectListItem { Value = e.Hid.ToString(), Text = e.Hname }).ToList();
return View(vm);
}
}
[HttpPost]
public async Task<IActionResult> Create(EMPVM vm)
{
using (SankarContext obj = new SankarContext())
{
if (ModelState.IsValid)
{
Emp emp = new Emp();
emp.Eid = vm.Eid ?? 0;
emp.Name = vm.Name;
emp.Address = vm.Address;
emp.Password = vm.Password;
emp.Gender = vm.Gender;
emp.Email = vm.Email;
emp.Salary = vm.Salary;
emp.Dob = vm.Dob;
emp.Cid = vm.Country;
emp.Sid = vm.State;
await obj.Emp.AddAsync(emp);
await obj.SaveChangesAsync();
await obj.Emphobbymap.AddRangeAsync(vm.Hobby.Select(e => new Emphobbymap { Eid = vm.Eid, Hid = e }).ToList());
await obj.SaveChangesAsync();
return RedirectToAction("Index");
}
else
{
vm.Countrylist = obj.Country.ToList().Select(e => new SelectListItem { Value = e.Cid.ToString(), Text = e.Cname }).ToList();
vm.Statelist = obj.State.Where(m => m.Cid == vm.Country.Value).ToList().Select(e => new SelectListItem { Value = e.Sid.ToString(), Text = e.Sname }).ToList();
vm.Hobbylist = obj.Hobby.ToList().Select(e => new SelectListItem { Value = e.Hid.ToString(), Text = e.Hname }).ToList();
return View(vm);
}
}
}
[HttpGet]
public async Task<IActionResult> Edit(int? id)
{
using (SankarContext obj = new SankarContext())
{
if (id == null)
return NoContent();
Emp emp = await obj.Emp.SingleOrDefaultAsync(e => e.Eid == id);
EMPVM vm = new EMPVM();
vm.Eid = emp.Eid;
vm.Name = emp.Name;
vm.Address = emp.Address;
vm.Gender = emp.Gender;
vm.Email = emp.Email;
vm.Salary = emp.Salary;
vm.Dob = emp.Dob;
vm.Country = emp.Cid;
vm.State = emp.Sid;
vm.Countrylist = obj.Country.Select(e => new SelectListItem { Value = e.Cid.ToString(), Text = e.Cname }).ToList();
vm.Statelist = obj.State.Where(e => e.Cid == emp.Cid).Select(e => new SelectListItem { Value = e.Sid.ToString(), Text = e.Sname }).ToList();
vm.Hobbylist = obj.Hobby.Select(e => new SelectListItem { Value = e.Hid.ToString(), Text = e.Hname }).ToList();
vm.Hobby = obj.Emphobbymap.Where(e => e.Eid == id).Select(m => m.Hid.Value).ToList();
return View(vm);
}
}
[HttpPost]
public async Task<IActionResult> Edit(EMPVM vm)
{
using (SankarContext obj = new SankarContext())
{
if (vm == null)
return NoContent();
ModelState.Remove("Password");
ModelState.Remove("ConfirmPassword");
if (ModelState.IsValid)
{
Emp emp = await obj.Emp.SingleOrDefaultAsync(e => e.Eid == vm.Eid);
emp.Name = vm.Name;
emp.Address = vm.Address;
emp.Password = vm.Password;
emp.Gender = vm.Gender;
emp.Email = vm.Email;
emp.Salary = vm.Salary;
emp.Dob = vm.Dob;
emp.Cid = vm.Country;
emp.Sid = vm.State;
obj.Emp.Update(emp);
await obj.SaveChangesAsync();
obj.Emphobbymap.RemoveRange(obj.Emphobbymap.Where(e => e.Eid == vm.Eid));
await obj.SaveChangesAsync();
await obj.Emphobbymap.AddRangeAsync(vm.Hobby.Select(e => new Emphobbymap { Eid = vm.Eid, Hid = e }).ToList());
await obj.SaveChangesAsync();
return RedirectToAction("Index");
}
else
{
vm.Countrylist = obj.Country.ToList().Select(e => new SelectListItem { Value = e.Cid.ToString(), Text = e.Cname }).ToList();
vm.Statelist = obj.State.Where(m => m.Cid == vm.Country.Value).ToList().Select(e => new SelectListItem { Value = e.Sid.ToString(), Text = e.Sname }).ToList();
vm.Hobbylist = obj.Hobby.ToList().Select(e => new SelectListItem { Value = e.Hid.ToString(), Text = e.Hname }).ToList();
return View(vm);
}
}
}
[HttpGet]
public async Task<IActionResult> Delete(int? id)
{
using (SankarContext obj = new SankarContext())
{
if (id == null)
return NoContent();
Emp emp = await obj.Emp.SingleOrDefaultAsync(e => e.Eid == id);
obj.Emp.Remove(emp);
await obj.SaveChangesAsync();
obj.Emphobbymap.RemoveRange(obj.Emphobbymap.Where(e => e.Eid == id));
await obj.SaveChangesAsync();
return RedirectToAction("Index");
}
}
}
}
Index view code :
@model IEnumerable<WebApplication1.DAL.Emp>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Salary)
</th>
<th>
@Html.DisplayNameFor(model => model.Dob)
</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
@Html.DisplayFor(modelItem => item.Dob)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Eid">Edit</a> |
<a asp-action="Delete" asp-route-id="@item.Eid">Delete</a>
</td>
</tr>
}
</tbody>
</table>
Create view code :
@model WebApplication1.ViewModel.EMPVM
@{
ViewData["Title"] = "Create";
}
<h2>Create</h2>
<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="Eid" class="control-label"></label>
<input asp-for="Eid" class="form-control" />
<span asp-validation-for="Eid" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Address" class="control-label"></label>
<textarea asp-for="Address" class="form-control"></textarea>
<span asp-validation-for="Address" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password" class="control-label"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ConfirmPassword" class="control-label"></label>
<input asp-for="ConfirmPassword" class="form-control" />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Gender" class="control-label"></label>
<input type="radio" value="Male" asp-for="Gender" />Male
<input type="radio" value="Female" asp-for="Gender" />Female
<span asp-validation-for="Gender" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Salary" class="control-label"></label>
<input asp-for="Salary" class="form-control" />
<span asp-validation-for="Salary" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Dob" class="control-label"></label>
<input type="text" asp-for="Dob" class="form-control" id="dob" />
<span asp-validation-for="Dob" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Country" class="control-label"></label>
<select asp-for="Country" id="ddlc" asp-items="Model.Countrylist" class="form-control">
<option value="">Select</option>
</select>
<span asp-validation-for="Country" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="State" class="control-label"></label>
<select asp-for="State" id="ddls" asp-items="Model.Statelist" class="form-control">
<option value="">Select</option>
</select>
<span asp-validation-for="State" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Hobby" class="control-label"></label>
<select asp-for="Hobby" multiple id="hb" asp-items="Model.Hobbylist" class="form-control"></select>
<span asp-validation-for="Hobby" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
<script type="text/javascript">
$(function () {
$('#ddlc').change(function () {
$.ajax({
url: '@Url.Action("fillddl", "Emp")',
data: { CID: $('#ddlc').val() },
type: 'GET',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (result) {
$('#ddls').empty();
$('#ddls').append("<option value=''>Select</option>");
$.each(JSON.parse(result), function (i, j) {
$('#ddls').append("<option value='" + j.Sid + "'>" + j.Sname + "</option>");
});
}
});
});
});
</script>
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Edit view code :
@model WebApplication1.ViewModel.EMPVM
@{
ViewData["Title"] = "Edit";
}
<h2>Edit</h2>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Eid" class="control-label"></label>
<input asp-for="Eid" class="form-control" />
<span asp-validation-for="Eid" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Address" class="control-label"></label>
<textarea asp-for="Address" class="form-control"></textarea>
<span asp-validation-for="Address" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Gender" class="control-label"></label>
<input type="radio" value="Male" asp-for="Gender" />Male
<input type="radio" value="Female" asp-for="Gender" />Female
<span asp-validation-for="Gender" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Salary" class="control-label"></label>
<input asp-for="Salary" class="form-control" />
<span asp-validation-for="Salary" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Dob" class="control-label"></label>
<input type="text" asp-for="Dob" class="form-control" id="dob" />
<span asp-validation-for="Dob" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Country" class="control-label"></label>
<select asp-for="Country" id="ddlc" asp-items="Model.Countrylist" class="form-control">
<option value="">Select</option>
</select>
<span asp-validation-for="Country" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="State" class="control-label"></label>
<select asp-for="State" id="ddls" asp-items="Model.Statelist" class="form-control">
<option value="">Select</option>
</select>
<span asp-validation-for="State" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Hobby" class="control-label"></label>
<select asp-for="Hobby" multiple id="hb" asp-items="Model.Hobbylist" class="form-control"></select>
<span asp-validation-for="Hobby" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-default" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
<script type="text/javascript">
$(function () {
$('#ddlc').change(function () {
$.getJSON('@Url.Action("fillddl", "Emp")', { CID: $('#ddlc').val() }, function (data1) {
$('#ddls').empty();
$('#ddls').append("<option value=''>Select</option>");
debugger;
$.each(JSON.parse(data1), function (i, j) {
$('#ddls').append("<option value='" + j.Sid + "'>" + j.Sname + "</option>");
});
});
});
});
</script>
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
No comments:
Post a Comment