Step 1. Create database Code:-
CREATE proc [dbo].[PrMST_User]
@UsersId int='',
@nMode int,
@UserFirstName nvarchar(100)='',
@UserLastName nvarchar(100)='',
@EmailId nvarchar(100)='',
@Phone nvarchar(20)='',
@Password nvarchar(100)='',
@Role int='',
@Alias nvarchar (50)='',
@Remark varchar(100)='',
@SortBy int='',
@Status int='',
@CreatedBy varchar(50)='',
@Clients_ID varchar(50)=''
as
begin
BEGIN TRY
BEGIN TRAN
DECLARE @ERR VARCHAR(1000)='',@cCMD VARCHAR(MAX),@ERRNO INT
IF @nMode=1--SAVE Data
BEGIN
INSERT tbl_AddUsers(UserFirstName,UserLastName,EmailId,Phone,Password,Role,Alias,Remark,SortBy,Status,CreatedBy,Create_Date,ModifiedBy,Modified)
SELECT @UserFirstName,@UserLastName,@EmailId,@Phone ,@Password,@Role,@Alias,@Remark,@SortBy,@Status,@CreatedBy,GETDATE(),'',NULL
END
IF @nMode=2--bind Daata
BEGIN
select tbl_AddUsers.*,role.UserRoleId, role.UserRoleName from tbl_AddUsers inner join tbl_AddUserRole as role on tbl_AddUsers.Role=role.UserRoleId
END
IF @nMode=3--bind Dropdown List
BEGIN
select UserRoleId,UserRoleName from tbl_AddUserRole
END
IF @nMode=4--Edit
BEGIN
select tbl_AddUsers.*,role.UserRoleId, role.UserRoleName from tbl_AddUsers inner join tbl_AddUserRole as role on tbl_AddUsers.Role=role.UserRoleId where UsersId=@UsersId
END
IF @nMode=5--Deltete
BEGIN
delete from tbl_AddUsers where UsersId = @UsersId
END
IF @nMode=6--Edit_Grid
BEGIN
--update Employee set name=@name,Gender=@Gender,country=@country,age=@age,address=@address where empid=@empid
update tbl_AddUsers set UserFirstName= @UserFirstName,UserLastName=@UserLastName,EmailId=@EmailId,Phone=@Phone ,Password=@Password,Role=@Role,Alias=@Alias,Remark=@Remark,
SortBy=@SortBy,Status=@Status,ModifiedBy=@CreatedBy,Modified=GETDATE() where UsersId=@UsersId
END
IF @nMode=7--bind Dropdown List Users
BEGIN
select UsersId,UserFirstName from tbl_AddUsers where CreatedBy = @Clients_ID
END
IF @nMode=8--bind Dropdown List Users
BEGIN
select ClientId ,UserFirstName from Registration where Clients_ID = @Clients_ID
END
END TRY
BEGIN CATCH
SELECT @ERRNO=@@ERROR ,@ERR=ERROR_MESSAGE()
PRINT @ERRNO
PRINT @ERR
--IF @ERR LIKE '%UsersId%'--@ERRNO=2627--PK
-- EXEC PrMST_ErrorHandler @nMode,@ERRNO,@ERR,'Alias',@Alias,@ERR OUTPUT
END CATCH
IF ISNULL(@ERR,'')=''
COMMIT
ELSE
ROLLBACK
SELECT @ERR ERR_MSG,CASE ISNULL(@ERR,'') WHEN '' THEN 'SUCCESS'
ELSE 'FAILED' END [STATUS]
EXT:
SET NOCOUNT OFF
END--Procedure PrMST_User Ends
GO
Step 2:- View Code:-
//----------------------------------------START-----------------------------------------------------------//
@model Mashin_Test.ViewModel.EmployeeViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div class="row">
@using (Html.BeginForm("SaveRecord", "Home", FormMethod.Post))
{
@Html.HiddenFor(x => x.Empid);
<div>
<table>
<tr>
<td>@Html.LabelFor(x => x.Name)</td>
<td>@Html.TextBoxFor(x => x.Name)</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.FkCid)</td>
<td>@Html.DropDownList("FkCid", ViewBag.Country as SelectList, "-select-")</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.Age)</td>
<td>@Html.TextBoxFor(x => x.Age)</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.Address)</td>
<td>@Html.TextBoxFor(x => x.Address)</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.Salary)</td>
<td>@Html.TextBoxFor(x => x.Salary)</td>
</tr>
<tr>
<td><input type="submit" id="btnsave" value="Save" /></td>
</tr>
</table>
</div>
}
<br />
@if(Model !=null)
{
@Html.Partial("_Index")
}
else
{
<div class="danger">Record Doed not Found !!</div>
}
</div>
//-------------------------------------------------END-------------------------------------------------//
Step : 3 - Partial Code.
//----------------------------------------------START--------------------------------------------------------//
@model Mashin_Test.ViewModel.EmployeeViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<div class="row">
@using (Html.BeginForm("SaveRecord", "Home", FormMethod.Post))
{
@Html.HiddenFor(x => x.Empid);
<div>
<table>
<tr>
<td>@Html.LabelFor(x => x.Name)</td>
<td>@Html.TextBoxFor(x => x.Name)</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.FkCid)</td>
<td>@Html.DropDownList("FkCid", ViewBag.Country as SelectList, "-select-")</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.Age)</td>
<td>@Html.TextBoxFor(x => x.Age)</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.Address)</td>
<td>@Html.TextBoxFor(x => x.Address)</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.Salary)</td>
<td>@Html.TextBoxFor(x => x.Salary)</td>
</tr>
<tr>
<td><input type="submit" id="btnsave" value="Save" /></td>
</tr>
</table>
</div>
}
<br />
@if(Model !=null)
{
@Html.Partial("_Index")
}
else
{
<div class="danger">Record Doed not Found !!</div>
}
</div>
//-----------------------------------------------END---------------------------------------------------------//
STEP 4: View Model Code:-
//-------------------------------------------START--------------------------------------------------------------//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Mashin_Test.Models;
namespace Mashin_Test.ViewModel
{
public class EmployeeViewModel
{
public int Empid { get; set; }
[StringLength(50)]
public string Name { get; set; }
public int? Age { get; set; }
[StringLength(50)]
public string Address { get; set; }
public double? Salary { get; set; }
public int? FkCid { get; set; }
public string Cname { get; set; }
public List<Emploeyee> EmpList { get; set; }
public List<EmployeeViewModel> EmpvmList { get; set; }
}
}
//------------------------------------------END-----------------------------------------------------------------//
Step 5: Controller Code-
//------------------------------------------START----------------------------------------------------------//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mashin_Test.Models;
using Mashin_Test.LogicLayer;
using Mashin_Test.ViewModel;
using Dapper;
using System.Data.Entity;
namespace Mashin_Test.Controllers
{
public class HomeController : Controller
{
private _dbconnection dd = new _dbconnection();
public ActionResult Index(string Id)
{
//------------------------------Country Bind----------------------------//
ViewBag.Country = new SelectList(dd.Countries.ToList(), "Cid", "Cname");
//-------------------------------End Country------------------------------------//
Emploeyee _objemp = new Emploeyee();
List<EmployeeViewModel> _Lstempvm = new List<EmployeeViewModel>();
EmployeeViewModel _objevm2 = new EmployeeViewModel();
if (!string.IsNullOrEmpty(Id))
{
int empid = Convert.ToInt16(Id);
_objemp = dd.Emploeyees.FirstOrDefault(x => x.Empid == empid);
_objevm2.Empid = empid;
_objevm2.Name = _objemp.Name;
_objevm2.Age = _objemp.Age;
_objevm2.Address = _objemp.Address;
_objevm2.Salary = _objemp.Salary;
_objevm2.FkCid = _objemp.FkCid;
}
try
{
List<Emploeyee> _emplist = dd.Emploeyees.ToList();
List<Country> _countrylist = dd.Countries.ToList();
var Query = (from a in _emplist
join b in _countrylist on a.FkCid equals b.Cid
select new
{
a.Empid,
a.Name,
a.Age,
a.Address,
a.Salary,
a.FkCid,
b.Cname,
}).ToList();
foreach (var item in Query)
{
EmployeeViewModel _objevm1 = new EmployeeViewModel();
_objevm1.Empid = item.Empid;
_objevm1.Name = item.Name;
_objevm1.Age = item.Age;
_objevm1.Address = item.Address;
_objevm1.Salary = item.Salary;
_objevm1.Cname = item.Cname;
_Lstempvm.Add(_objevm1);
}
_objevm2.EmpvmList = _Lstempvm;
}
catch (Exception ex)
{
HttpContext.Response.Write(ex.Message);
}
return View(_objevm2);
}
public ActionResult Delete(string Id)
{
Emploeyee emp = new Emploeyee();
int empid = Convert.ToInt16(Id);
emp = dd.Emploeyees.Find(empid);
dd.Emploeyees.Remove(emp);
dd.SaveChanges();
return RedirectToAction("Index");
}
[HttpPost]
public ActionResult SaveRecord(EmployeeViewModel Emps)
{
Emploeyee Emp = new Emploeyee();
try
{
//EmployeeLogicLayer emp = new EmployeeLogicLayer();
//dynamic tt = emp.SaveRecord(Emps);
Emp.Empid = Emps.Empid;
Emp.Name = Emps.Name;
Emp.Age = Emps.Age;
Emp.Address = Emps.Address;
Emp.Salary = Emps.Salary;
Emp.FkCid = Emps.FkCid;
if (Emp.Empid == 0)
{
dd.Emploeyees.Add(Emp);
}
else
{
dd.Entry(Emp).State = EntityState.Modified;
}
dd.SaveChanges();
}
catch (Exception ex)
{
throw ex;
}
return Redirect("Index");
}
}
}
//------------------------------------------END-------------------------------------------------------------//
Step 6: Logic Layer. If you want to do With Stored Procedure.
//------------------------------------------------------START-------------------------------------------------//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Dapper;
using Mashin_Test.Models;
using Mashin_Test.ViewModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Mashin_Test.LogicLayer
{
public class EmployeeLogicLayer
{
private _dbconnection dd = new _dbconnection();
private IDbConnection idb = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
public dynamic SaveRecord(EmployeeViewModel Emps)
{
dynamic result;
var tt = new DynamicParameters();
int nmode = 1;
try
{
tt.Add("@nmode", nmode);
tt.Add("@Name", Emps.Name);
tt.Add("@Age",Emps.Age);
tt.Add("@Address",Emps.Address);
tt.Add("@Salary",Emps.Salary);
tt.Add("@Fkcid",Emps.FkCid);
result = this.idb.Query<dynamic>("USP_Employee", tt, commandType: CommandType.StoredProcedure);
if (result.Count > 0)
{
foreach (var item in result)
{
if (item.err_msg == "")
{
result = "Saved";
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return result;
}
}
}
//---------------------------------------------------------END------------------------------------------------//
No comments:
Post a Comment