Wednesday, 19 April 2017

Difference between RAZOR View Engine and ASPX View Engine

What is View Engine??

The view-engine is responsible for creating HTML from your views. Views are usually some kind of mix-up of HTML and a programming language(ie. C# or VB)
The view engine is what’s responsible for rendering your view, and converting your code into glorious HTML.

In ASP.NET MVC, a View Engine translates view into HTML by:

    Providing implementation of –

  • IViewEngine (as template provider)
  •  IView (as rendering template) and
  • Template Engine(ie Razor or aspx ) for parsing and compiling view file into executable code.
There are many view engines available and some of them are following:
  1. ASPX
  2. Razor
  3. Spark
  4. NHaml
  5. NDJango
  6. Hasic
  7. Brail
  8. Bellevue
  9. Sharp Tiles
  10. String Template
  11. Wing Beats
  12. SharpDOM
    Currently most developers prefer to use Razor view engine as it provides very convenient way of programming. All of these view engines may not support ASP.NET MVC , it supports the following 2 view engines:
    1. Razor View Engine        Razor is a markup syntax that allows you to embed server-side code (written in C# or VB) in an HTML markup.A file containing server-side code in the C# syntax has an extension of .cshtml. However, a file containing server-side code in VB has an extension of .vbhtml.
    2. Web Form/Aspx View Engine  ASPX or the Web Forms engine is the default view engine that is included in the MVC Framework since the beginning. Writing a code with this engine is similar to writing a code in ASP.NET Web Forms.

    Understanding Syntax Difference

Being software developer, we are normally concerned about code syntax differences when using either of these two View Engines. So, in order to get better understanding, please look into following code snippet written using both ASPX and Razor View Engine in order.
aspx

Detailed Difference Between ASPX View Engine and Razor View Engine

diff2


diff1

Sunday, 16 April 2017

Working With Multiple tables

In this session we will  Discuss Working with Two tables in ASPMVC

Here i have Two tables in Database  1)- EmpCategory   2). EmpList
joinedImage
In EmpCat table we have List of Departments  and EmpList table we have List of all the Employees .using this table I want to Display a MVC Application that should Display Department List and Employee List Page .
Department table Will Display all the Departments that are available in as Hyperlink that should Redirect EmployeeList Page that display Name of All Employees belonging to the Department As HyperLink that should Redirect To Employee Details Page that should Display complete Details of particular Employee.

 Flow of Program (This is What we want to Achieve) ——

DEmo

Database Code-----

Create Database Demo
use Demo
Create table EmpCategory
(
EmpId int identity primary key,
EmpStream varchar(50)
)
Create table EmpList
(
EID int identity Primary key,
EName varchar(50),
EAge int,
EEmail varchar(50),
EmpCat int
)
insert into EmpCategory values(‘IT’)
insert into EmpCategory values(‘HR’)
insert into EmpCategory values(‘Payroll’)
insert into EmpList values(‘Manish’,19,’M@gmail.com’,1)
insert into EmpList values(‘Ravi’,22,’R@gmail.com’,3)
insert into EmpList values(‘Amit’,23,’A@gmail.com’,1)
insert into EmpList values(‘Riya’,23,’Riya@gmail.com’,2)
insert into EmpList values(‘Akanksha’,20,’Ak@gmail.com’,2)

Lets See how to implement this–

Follow these Steps to Implement the Program-
Step 1 –  Open your Visual Studio and Create a New MVC4 Program “WorkWithTwoTable”.
Go to File -> New Project -> Web ->ASP.Net MVC 4 Web Application  (Named “WorkWithTwoTable”) -> Ok ->Basic Template  ->ok
Step 2 – Add  Model Classes To map With Database Tables , first you have to Add “Department.cs”
Go to Solution Explorer -> Right Click on Models Folder ->Add ->Class (Department) ->
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace WorkWithTwoTables
{
[Table("EmpCategory")]
public class Department
{
[Key]
public int EmpId { get; set; }
public string EmpStream { get; set; }
}
}
Now Add Another Class Employee.cs to  map with  EmpList  table-
Go to Solution Explorer -> Right Click on Models Folder ->Add ->Class (Employee) ->
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace WorkWithTwoTables.Models
{
[Table("EmpList")]
public class Employee
{
[Key]
public int EID { get; set; }
public string EName { get; set; }
public int EAge { get; set; }
public string EEmail { get; set; }
public int EmpCat { get; set; }
}
}
Step 3 – Add  Context Classes to Models Folder , “EmployeeContext.cs”  //To  Query From Database
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace WorkWithTwoTables.Models
{
public class EmployeeContext:DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<Department> Departments { get; set; }
}
}
Step 4 – Now Create a Controller Class To Render Department Regarding Data on View .
Go to Solution Explorer -> Right Click on Controllers Folder ->Add ->Controller (DepartmentController) ->
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WorkWithTwoTables.Models;

namespace WorkWithTwoTables.Controllers
{
public class DepartmentController : Controller
{
EmployeeContext db = new EmployeeContext();
public ActionResult Index()
{
return View(db.Departments.ToList());
}
}
}
Step 5 – Now Create a  a View Page of Index

Right Click in Index Action Method->Add View->Index-> Ok..
@model List<WorkWithTwoTables.Models.Department>

@{
ViewBag.Title = "Index";
}
<h1>Department List</h1>
@foreach (var p in Model) 
{ 
@Html.ActionLink(p.EmpStream, "Index", "Employee", new {id=p.EmpId },null) 
}

Step 6 – Now Create a   Employee Controller ..

Go to Solution Explorer -> Right Click on Controllers Folder ->Add ->Controller (EmployeeController) ->
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WorkWithTwoTables.Models;
namespace WorkWithTwoTables.Controllers
{
public class EmployeeController : Controller
{
EmployeeContext db = new EmployeeContext();
public ActionResult Index(int id)
{
return View(db.Employees.Where(x=>x.EmpCat==id).ToList());
}
}
}
Step 7 – Add View of This Action Method ..

Right Click  in Action Method ->Add View->Index.cshtml-> Ok
@model List<workingWithTwoTables.Models.Employee>
@{
ViewBag.Title = "Index";
}
<h1>Employee List</h1>
@foreach (var p in Model) 
{
 @Html.ActionLink(p.EName, "Details", "Employee", new{ id=p.EmpCat},null)
 }

Step-8) Now Create a Details Action Method on Employee Controller ..
public ActionResult Details(int id)
{
return View(db.Employees.Single(x=>x.EID==id));
}
Step -9). Add A View Of Details Method
@model WorkWithTwoTables.Models.Employee

@{
 ViewBag.Title = "Details";
}
<h2>Details</h2>
<table>
 <tr>
 <th>Employee ID</th>
 <td>@Model.EID</td>
 </tr>
 <tr>
 <th>Employee Name</th>
 <td>@Model.EName</td>
 </tr>
 <tr>
 <th>Employee Age</th>
 <td>@Model.EAge</td>
 </tr>
 <tr>
 <th>Employee Email</th>
 <td>@Model.EEmail</td>
 </tr>
 
</table>

Step 10 - Add connection String in Web.Config File

<add name="EmployeeContext" connectionString="Data Source=DHANANJAY\SQLEXPRESS;Initial Catalog=Demo;User Id=sa;Password=1" providerName="System.Data.SqlClient"/>

 Step 11 -Edit in Global.asax File       

 Use Namespace-  using System.Data.Entity;                                              Database.SetInitializer<WorkingWithTwoTable.Models.EmployeeContext>(null);

Step-12 RUN the Programm!!!!

Wednesday, 12 April 2017

Working With Joined Tables in ASPMVC

How To Render Two Tables joined Data on a single View

In the previous  article “Working With Single table” We have already done the from where a user will get How To Interact with Single Table. Here in this Article , we will create a Page Rendering Data by using  joined Operator .
Follow the following steps in order to implement “Part 2: Working With Tables”.
Step-1 : Create Following tables in SQL ServerjoinedImageHere I have Created 2 tables – 1) First table that you are watching in image is EmpCat       Table.   2) Second Table that you are watching in image  is EmpList. 3) Third Image is the Output after Applying join operator,is the result that i want to Render on View Page.
Click To Link for Database Code- DatabaseCode.
Step-2) Use Database First Approach to Add Both Table in ASPMVC Application.
Go to Solution Explorer -> Right Click on Models Folder ->Add -> New Item ->Data ->ADO.NET Entity DataModel .
Step-3) You have to create a Custom Model class in Models Folder to Render join Data in a View.
Go to Solution Explorer -> Right Click on Models Folder ->Add -> Class ->Write the Name of class “MyViewModel
public class MyViewModel
{
    public EmpCat Cat { get; set; }
    public EmpList emp { get; set; }
}
This being said I would strongly recommend you to use view models and strongly typed views instead of ViewBag. This way you will also get Intellisense in your view which would have helped you pick the correct names.
Step-4) Now You have to create a Controller Action Method Index  Controllers  Folder to Render join Data in a View.
Go to Solution Explorer -> Right Click on Controllers  Folder ->Add -> Controller  ->Write the Name of Controller “HomeController“.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication51.Models;

namespace MvcApplication51.Controllers
{
    public class HomeController : Controller
    {
        DemoEntities db = new DemoEntities();       
       public ActionResult Index()
{
    var viewModel = 
        from pd in db.EmpCats
        join p in db.EmpLists on pd.EmpId equals p.EmpCat
        orderby p.EName
        select new MyViewModel 
             { 
                Cat = pd, emp = p 
             };
    return View(viewModel.ToList());
}
Step-5)and finally inside your strongly typed view use the view model:
@model List<MvcApplication51.Models.MyViewModel>
<table>
   <tr>
      <td>EmployeeName</td>
      <td>EmployeeAge</td>
      <td>EmployeeEmail</td>
      <td>Employee Category</td>  </tr>  
@foreach (var item in Model)
{
   <tr>
       <td>@item.emp.EName</td>
       <td>@item.emp.EAge</td>
       <td>@item.emp.EEmail</td>
       <td>@item.Cat.EmpStream</td>
   </tr>
}
</table>

Recent Post