Business ERP Part 41 Add Branch Focal Person 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 Add Branch Focal Person, 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 CreateBranchFocalPerson(int? branchID)
{
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 branch = DB.tblBranches.Find(branchID);
var focalperonmv = new FocalPersonMV();
focalperonmv.employeeMV = new EmployeeMV();
focalperonmv.userMV = new UserMV();
focalperonmv.BranchID = branch.BranchID;
focalperonmv.CompanyID = branch.CompanyID;
focalperonmv.userMV.UserTypeID = 3;
return View(focalperonmv);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateBranchFocalPerson(FocalPersonMV focalpersonmv)
{
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);
using (var transaction = DB.Database.BeginTransaction())
{
try
{
if (ModelState.IsValid)
{
var newuser = new tblUser();
newuser.UserTypeID = 3;
newuser.FullName = focalpersonmv.employeeMV.Name;
newuser.Email = focalpersonmv.employeeMV.Email;
newuser.ContactNo = focalpersonmv.employeeMV.ContactNo;
newuser.UserName = focalpersonmv.userMV.UserName;
newuser.Password = focalpersonmv.userMV.Password;
newuser.IsActive = true;
newuser.Address = focalpersonmv.employeeMV.Address;
DB.tblUsers.Add(newuser);
DB.SaveChanges();
var newemployee = new tblEmployee();
newemployee.Address = focalpersonmv.employeeMV.Address;
newemployee.BranchID = focalpersonmv.BranchID;
newemployee.CNIC = focalpersonmv.employeeMV.CNIC;
newemployee.CompanyID = focalpersonmv.CompanyID;
newemployee.ContactNo = focalpersonmv.employeeMV.ContactNo;
newemployee.Description = focalpersonmv.employeeMV.Description;
newemployee.Designation = focalpersonmv.employeeMV.Designation;
newemployee.Email = focalpersonmv.employeeMV.Email;
newemployee.MonthlySalary = focalpersonmv.employeeMV.MonthlySalary;
newemployee.Name = focalpersonmv.employeeMV.Name;
newemployee.Photo = "~/Content/Template/img/user/employee.png";
newemployee.UserID = newuser.UserID;
DB.tblEmployees.Add(newemployee);
DB.SaveChanges();
transaction.Commit();
}
return RedirectToAction("AllCompanyBranchs", "Branch");
}
catch (Exception ex)
{
transaction.Rollback();
ModelState.AddModelError("Name", "Must be Filled All Field with correct data!");
return View(focalpersonmv);
}
}
}
}
}
View Code :
@model ERP_App.Models.FocalPersonMV
@{
ViewBag.Title = "Branch Focal Person";
}
<div class="col-lg-6">
<div class="card card-default mb-6">
<div class="card-header">Branch Focal Person</div>
<div class="card-body">
@using (Html.BeginForm("CreateBranchFocalPerson", "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.userMV.UserTypeID)
<h3>Person Details</h3>
<hr />
<div class="form-group">
@Html.LabelFor(model => model.employeeMV.Name, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.employeeMV.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.employeeMV.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.employeeMV.ContactNo, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.employeeMV.ContactNo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.employeeMV.ContactNo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.employeeMV.Photo, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.employeeMV.Photo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.employeeMV.Photo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.employeeMV.Email, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.employeeMV.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.employeeMV.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.employeeMV.Address, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.employeeMV.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.employeeMV.Address, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.employeeMV.CNIC, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.employeeMV.CNIC, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.employeeMV.CNIC, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.employeeMV.Designation, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.employeeMV.Designation, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.employeeMV.Designation, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.employeeMV.Description, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.employeeMV.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.employeeMV.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.employeeMV.MonthlySalary, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.employeeMV.MonthlySalary, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.employeeMV.MonthlySalary, "", new { @class = "text-danger" })
</div>
</div>
<hr />
<h3>User Details</h3>
<hr />
<div class="form-group">
@Html.LabelFor(model => model.userMV.UserName, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.userMV.UserName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.userMV.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.userMV.Password, htmlAttributes: new { @class = "control-label col-md-6" })
<div class="col-md-10">
@Html.EditorFor(model => model.userMV.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.userMV.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-outline-success" />
@Html.ActionLink("Back", "AllCompanyBranchs" ,"Branch", null, new { @class = "btn btn-outline-default" })
</div>
</div>
</div>
}
</div>
</div>
</div>
Comments
Post a Comment