Sunday, January 29, 2017

How to use AutoMapper

Here in this Post I will show How to use AutoMapper.
Before that what is the necessity to use AutoMapper, when we are using ViewModels in Asp.Net MVC we need to map each and every column individually.

Suppose take a table of Employee with multiple columns as shown.
(Table) Employee: ID, FirstName, LastName, Gender, Salary, DOJ and DeptID

(ViewModel) EmployeeVM:

public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public DateTime DOJ { get; set; }
public int DeptID { get; set; }

To Insert New Employee, we need to map (ViewModel) EmployeeVM and (Model) Employee as shown below.
  

public void ADDEmployee(EmployeeVM e)
{
 Employee emp = new Employee();
 emp.ID = e.ID;
 emp.FirstName = e.FirstName;
 emp.LastName = e.LastName;
 emp.Gender = e.Gender;
 emp.Salary = e.Salary;
 emp.DOJ = e.DOJ;

 ent.Employees.Add(emp);
 ent.SaveChanges();
}

Supplose the Employee table had more columns like 20+... We need to map them one by one. Instead of doing that manually we will use AutoMapper.
Now Lets see how to use it for the above Employee table and EmployeeVM (ViewModel).

CRUD operations using Bootstrap Modal in Asp.Net MVC

In this post I will show how to CRUD operations in Asp.Net MVC using Bootstrap Modal and display records using Jquery DataTables with Exporting and printing.

Previously I had shown how to....
MVC:
Output:

Sunday, January 22, 2017

Display message when user is not authorized in mvc

In this post I will show how to Display Error message when user is not Authorized, Continuing the previous POST i.e.,
Customizing Authorize attribute or Role based Authentication or Authorization in MVC

Previously I had shown how to....
DataTables in MVC:
OutPut:

Customizing Authorize attribute or Role based Authentication or Authorization in MVC

As the TITLE says Customizing Authorize attribute, In this post I will show how to create a Customized Authorize attribute and use it. This is used to redirect the user to Home Page when he/she is authenticated and not authorized i.e, when the Authorize attribute generated 401 response it will redirect to Login page.
Previously I had shown how to...
DataTables in MVC:
AngularJS:
By creating a CustomAuthorize class which inherits AuthorizeAttribute we can redirect the user to home page instead of a login page.
How to use?
  

[CustomAuthorize(Roles = "Admin")]
public ActionResult Index()
{
 return View();
}