Business ERP Part 39 Edit Employee in ASP.NET MVC
Hi, Dear's here we learn how to implement Auto Time Table Generator in Visual Studio using C# Windows Form. Ilyasoft software company provide full project step by step training on our YouTube Channel ilyasoft software company so now subscribe, share and like for more project base tutorials
In this part we are going to Implement to edit Branch employee to tblEmployeeTable, so now follow below code.
Source Code:
Action C# Code :
using DatabaseLayer;
using ERP_App.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ERP_App.Controllers
{
public class AdminConfigController : Controller
{
private BusinessERPDbEntities DB = new BusinessERPDbEntities();
public ActionResult EditBranchEmployee(int? employeeid)
{
if (string.IsNullOrEmpty(Convert.ToString(Session["UserName"])))
{
return RedirectToAction("Login", "Home");
}
var userid = 0;
var usertypeid = 0;
var companyid = 0;
var branchid = 0;
var branchtypeid = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
int.TryParse(Convert.ToString(Session["UserTypeID"]), out usertypeid);
int.TryParse(Convert.ToString(Session["CompanyID"]), out companyid);
int.TryParse(Convert.ToString(Session["BranchID"]), out branchid);
int.TryParse(Convert.ToString(Session["BranchTypeID"]), out branchtypeid);
var employee = DB.tblEmployees.Find(employeeid);
var editemployee = new EmployeeMV();
editemployee.Address = employee.Address;
editemployee.BranchID = employee.BranchID;
editemployee.CNIC = employee.CNIC;
editemployee.CompanyID = employee.CompanyID;
editemployee.ContactNo = employee.ContactNo;
editemployee.Description = employee.Description;
editemployee.Designation = employee.Designation;
editemployee.Email = employee.Email;
editemployee.EmployeeID = employee.EmployeeID;
editemployee.MonthlySalary = employee.MonthlySalary;
editemployee.Name = employee.Name;
editemployee.Photo = employee.Photo;
editemployee.UserID = employee.UserID;
return View(editemployee);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditBranchEmployee(EmployeeMV employeemv)
{
if (string.IsNullOrEmpty(Convert.ToString(Session["UserName"])))
{
return RedirectToAction("Login", "Home");
}
var userid = 0;
var usertypeid = 0;
var companyid = 0;
var branchid = 0;
var branchtypeid = 0;
int.TryParse(Convert.ToString(Session["UserID"]), out userid);
int.TryParse(Convert.ToString(Session["UserTypeID"]), out usertypeid);
int.TryParse(Convert.ToString(Session["CompanyID"]), out companyid);
int.TryParse(Convert.ToString(Session["BranchID"]), out branchid);
int.TryParse(Convert.ToString(Session["BranchTypeID"]), out branchtypeid);
try
{
if (ModelState.IsValid)
{
var checkemployee = DB.tblEmployees.Where(e => e.CNIC == employeemv.CNIC.Trim() && e.EmployeeID != employeemv.EmployeeID).FirstOrDefault();
if (checkemployee == null)
{
var editemployee = DB.tblEmployees.Find(employeemv.EmployeeID);
editemployee.Address = employeemv.Address;
editemployee.CNIC = employeemv.CNIC;
editemployee.ContactNo = employeemv.ContactNo;
editemployee.Description = employeemv.Description;
editemployee.Designation = employeemv.Designation;
editemployee.Email = employeemv.Email;
editemployee.MonthlySalary = employeemv.MonthlySalary;
editemployee.Name = employeemv.Name;
//newemployee.Photo = "~/Content/Template/img/user/employee.png";
DB.Entry(editemployee).State = System.Data.Entity.EntityState.Modified;
DB.SaveChanges();
return RedirectToAction("BranchEmployees");
}
else
{
ModelState.AddModelError("CNIC", "Already Exist!");
}
}
return View(employeemv);
}
catch (Exception ex)
{
ModelState.AddModelError("Name", "Must be Filled All Field with correct data!");
return View(employeemv);
}
}
}
}
View Code :
@model ERP_App.Models.EmployeeMV
@{
ViewBag.Title = "Edit Employee";
}
<div class="col-lg-6">
<div class="card card-default mb-6">
<div class="card-header">Edit Employee</div>
<div class="card-body">
@using (Html.BeginForm("EditBranchEmployee", "Employee", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(u => u.BranchID)
@Html.HiddenFor(u => u.CompanyID)
@Html.HiddenFor(u => u.UserID)
@Html.HiddenFor(u=>u.EmployeeID)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ContactNo, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.ContactNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ContactNo, "", new { @class = "text-danger" })
</div>
</div>
@*<div class="form-group">
@Html.LabelFor(model => model.Photo, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.Photo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Photo, "", new { @class = "text-danger" })
</div>
</div>*@
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CNIC, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.CNIC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CNIC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Designation, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.Designation, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Designation, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MonthlySalary, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.MonthlySalary, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MonthlySalary, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Update" class="btn btn-outline-success" />
@Html.ActionLink("Back", "BranchEmployees", null, new { @class = "btn btn-outline-default" })
</div>
</div>
</div>
}
</div>
</div>
</div>
Comments
Post a Comment