Entity Framework with MVC WebApp.

Assalamualaykum Wr Br..:)
Today we build MVC web app. with Entity Framework. Lets add new table TblEmployee in EF_DBFirst database as shown below:
The respective script is:
CREATE TABLE [dbo].[TblEmployee] (
[Id]     INT          NOT NULL,
[Name]   VARCHAR (50) NOT NULL,
[Gender] VARCHAR (50) NOT NULL,
[City]   VARCHAR (50) NOT NULL,
Now create new application of MVC_EF and add new controller EmployeeController.cs, Employee.cs in Model, EmployeeContext.cs in Model and Display.cshtml in Views/Employee. As per the table design generate a class with name Employee.cs in Model as shown below:
In above class, Table attribute describes class Employee as per the table TblEmployee and Table attribute exists in System.ComponentModel.DataAnnotations.Schema namespace. Now we need to create connection string which is done in web.config as shown:
Please notice that we have provided name=”EmployeeContext”hence we create a dbcontext class as shown below with name EmployeeContext.cs where we define property which returns whole data of table employee.
Now focus on controller class which is responsible for routing the user requests and response. Design the controller class as shown below:
In controller we create employeecontext class object and access single record with respect to id passed from frontend as a parameter to Details Controller. The Linq query is                                      (emp => emp.Id == id)
emp is temporary variable which cross checks with parameter id with existing db Id, if matches then displays data of record and returns to view. view is designed as shown:
the first line of above states that this view accesses model Employee and @Model.Name is retrieved from db table. Finally if i pass the url the following output is seen.


Thats all for the day.Jazakallah Khair.


Asp.Net Areas

Assalamualaykum Wr Br..:)

Today we explore the Asp.Net Area Concept with a small project. Lets look few theory points:

Areas provide a way to separate a large MVC Web application into smaller functional groupings. An area is effectively an MVC structure inside an application. An application could contain several MVC structures (areas).

To accommodate large projects, ASP.NET MVC lets you partition Web applications into smaller units that are referred to as areas.

For example, a single large e-commerce application might be divided into areas that represent the storefront, product reviews, user account administration, and the purchasing system. Each area represents a separate function of the overall application.

This walkthrough demonstrates how to implement areas in an ASP.NET MVC application. The walkthrough creates the functional framework for a blog site that has the following areas:

  • Main. This is entry point to the Web application. This area includes the landing page and a log-in feature.
  • Blog. This area is used to display blog posts and to search the archive.
  • Dashboard. This area is used to create and edit blog posts.

To keep this tutorial simple, the areas do not contain logic to perform the actual tasks for the blog.

Creating the Application Structure

To begin, you will create an ASP.NET MVC project and add the folder structure for two child areas (Blog and Dashboard).

To create the application structure

  1. In Visual Studio, in the File menu, and click New Project.
  2. In the Project types window, expand the Visual Basic node or the Visual C# node, and then select the Web node.
  3. In the Templates window, select ASP.NET MVC 2 Web Application.
  4. Name the project MvcAreasApplication, set the project location, and then select the Create directory for solution check box.
  5. Click OK.
  6. In Solution Explorer, right-click the project name, click Add, and then click Area.
  7. In Area Name, type Blog and then click Add.

    An Areas folder is added to the project. The Areas folder contains a folder structure that allows each child area to have its own models, views, and controllers.

  8. In Solution Explorer, right-click the project name, click Add, and then click Area.
  9. In Area Name, enter Dashboard and then click Add.

    When you are done, the Areas folder contains two child areas, Blog and Dashboard.

    Adding Area-Specific Controllers

     You will now add area-enabled controllers and action methods for each area.

    To add area controllers

    1. In Solution Explorer, right-click the Controllers subfolder for the Blog area, click Add, and then click Controller.
    2. Name the controller BlogController and then click Add.
    3. Add the following code to the BlogController class.

My Simple App MVC1

Assalamualikum Here we gonna explore first simple app of MVC(Model View Controller).
MVC stands for Model-View-Controller, a design pattern that’s very popular in the web development space. As an alternative to Web Forms, ASP.NET MVCtakes a different approach when it comes to structuring web applications. This means you won’t be dealing with ASPX pages and controls, postbacks or view state, or complicated event lifecycles. Instead, you’ll be defining controllers, actions, and views. The underlying ASP.NETplatform is
the same, however, so things like HTTPhandlers and HTTPmodules still apply, and you can mix MVCand Web Forms pages in the same application. Both ASP.NETWeb Forms and ASP.NET MVCsit alongside each other on top of the core ASP.NETplatform.
The model—The domain that your software is built around.
The view—The visual representation of a model, given some context. It’s usually the resulting markup that the framework renders to the browser.
The controller—The coordinator that provides the link between the view and the model. The controller is responsible for processing input, acting upon the model, and deciding on what action should be performed, such as rendering a view or redirecting to another page.
Now lets start our new project named “MY MVC1” and select C# language then select ASP.NET MVC project then a window gets populated select Empty project.
2. Observe that we find additional directories in solution as “Controller,Model,View and Content” which represents our ASP.NET MVC solution. Right Click on Controller then select New Controller and Give name as MYMVC1Cotroller…
3. A New class gets created inside Controller directory with a default method named as “Index” whose return type is ACTIONRESULT which is View.
4. Right click your Action Result method and select to create new view then a new window populates which asks to use razor view engine(CSHTML) or asp c#(ASPX) then select ASPX.So your controller should be like as shown below:
now lets work with view and should be like as below:
Now when you execute error is encountered because the controller is need to be set which is shown as in the url..
Thats all for the day…We shall continue it at some other time till then good Bye..