Business ERP Part 38 Register Employee in ASP.NET MVC

 Business ERP Part 38 Register 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 Register 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 NewBranchEmployee()
        {
            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 = new EmployeeMV();
            employee.CompanyID = companyid;
            employee.BranchID = branchid;
            employee.UserID = 0;
            return View(employee);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult NewBranchEmployee(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()).FirstOrDefault();
                    if (checkemployee == null)
                    {
                        var newemployee = new tblEmployee();
                        newemployee.Address = employeemv.Address;
                        newemployee.BranchID = branchid;
                        newemployee.CNIC = employeemv.CNIC;
                        newemployee.CompanyID = companyid;
                        newemployee.ContactNo = employeemv.ContactNo;
                        newemployee.Description = employeemv.Description;
                        newemployee.Designation = employeemv.Designation;
                        newemployee.Email = employeemv.Email;
                        newemployee.MonthlySalary = employeemv.MonthlySalary;
                        newemployee.Name = employeemv.Name;
                        newemployee.Photo = "~/Content/Template/img/user/employee.png";
                        newemployee.UserID = employeemv.UserID;
                        DB.tblEmployees.Add(newemployee);
                        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 = "New Employee";
}
<div class="col-lg-6">
    <div class="card card-default mb-6">
        <div class="card-header">New Employee</div>
        <div class="card-body">
            @using (Html.BeginForm("NewBranchEmployee", "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)
                    <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="Create" class="btn btn-outline-success" />
                            @Html.ActionLink("Back", "BranchEmployees", null, new { @class = "btn btn-outline-default" })
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>
</div>


Comments