Click here to Skip to main content
11,925,381 members (64,795 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked

Entity Framework Implementation Using ASP.NET MVP

, 13 May 2011 GPL3
Rate this:
Please Sign up or sign in to vote.
Entity Framework Implementation using ASP.NET MVP


ASP.NET MVP pattern evolved during the late 90s. It is the descendent of MVC pattern with a slight change in the design pattern, i.e., it implements the observer design approach of MVC. The rest of the components remain the same which means the model and view do their task as in MVC.

MVP (Model-View-Presenter)

ASP.NET MVP consists of three parts. They are:

  1. Model
  2. View
  3. Presenter


Model is the domain type or the business level object. It contains the application data and provides logics and behaviours to access the data. Model needs to be independent and shouldn’t have any problems and issues regarding the user interface (UI). Since the model should be independent, the same model can be used by different type of UI level applications.


A view is a place holder which holds the model data and shows them to the application users. Simply, view is a web form for presenting the model data. Model doesn’t have direct link with views while view should have knowledge about their models.


Presenter addresses the user input and uses it to manipulate the model data. A view passes the user input actions to the presenter for interpretation. Presenter will act upon the received data from the view and communicate with model and returns result to the view.
The following figure describes the basic flow of MVP pattern.

Fig. MVP architecture

MVP Vs. Web Forms Vs. MVC

Web forms have high productivity and less control over markup, URL and code. MVC (Model View Controller) has high control on markup, URL and code and less productivity. Here comes the MVP which balances between the productivity and control.

Choosing Between MVC and MVP

In MVC, the model stores the data, view is the representation of that data and the controller allows the user to change the data. When the data is changed, all the views are notified of the change and they put themselves as necessary. MVP is a derivative of MVC.

MVP gives less control to the view and puts most of the responsibility in the presenter. It is an attempt to make all logic part of the presenter, thus the view is “passive”. The basic idea of the MVC and MVP is the same, the model stores the data, the view is representation of that data and the presenter coordinates the data.

Entity Framework Introduction

In the real world programming, the database developers prefer to use relational model to represent data while the application developers prefer to use object oriented models. This difference in two models is addressed by ADO.NET Entity Framework. The ADO.NET Entity Framework is a descendent of ADO.NET to provide object relational mapping between conceptual entities which are .NET classes and the data store. Entity framework uses entity data model which consists of storage schema, the conceptual schema, the mapping schema and the entity classes.

Process Flow

  1. At first, create a new ASP.NET empty web application in a VWD (Visual Web Developer). In my case, I have used VS 2010.
  2. After that, add a new interface for view names IView in file IView.cs. The code looks like this:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using TestMVPApp.Model;
    namespace TestMVPApp.View
        public interface IView
            string FirstName { get; set; }
            string MiddleName { get; set; }
            string LastName { get; set; }
            string Address { get; set; }
            string ContactNo { get; set; }
            string Message { get; set; }
  3. Add a presenter class and name it as Presenter.cs which consists of the following code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using TestMVPApp.Model;
    using TestMVPApp.View;
    using System.Web;
    using System.Configuration;
    namespace TestMVPApp.Presenter
    	public class TestPresenter
    		IView detailsView;
    		public TestPresenter(IView view)
    			detailsView = view;
    		public string SaveDetails()
    			TestModel test = new TestModel();
    			if (test.SaveDetails(detailsView.FirstName, 
    				detailsView.MiddleName, detailsView.LastName, 
    				detailsView.Address, detailsView.ContactNo))
    				detailsView.Message = "Success";
    				detailsView.Message = "Failure";
    			detailsView.FirstName = string.Empty;
    			detailsView.MiddleName = string.Empty;
    			detailsView.LastName = string.Empty;
    			detailsView.Address = string.Empty;
    			detailsView.ContactNo = string.Empty;
    			return detailsView.Message.ToString();
  4. After adding presenter class, add a view page and name it as InputViewPage.aspx. In this UI page, create a table and fields for entering first name, middle name, last name, address and contact number.

    And in the code behind, write the code to implement the interface IView. The code for that is:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using TestMVPApp.Presenter;
    namespace TestMVPApp.View
        public partial class InputViewPage : System.Web.UI.Page, IView
            protected void Page_Load(object sender, EventArgs e)
            protected void btnShowData_Click(object sender, EventArgs e)
                TestPresenter detailsPresenter = new TestPresenter(this);
                if (detailsPresenter.SaveDetails() != null)
            public string FirstName
                get { return txtFname.Text; }
                set { txtFname.Text = value; }
            public string MiddleName
                get { return txtMName.Text; }
                set { txtMName.Text = value; }
            public string LastName
                get { return txtLName.Text; }
                set { txtLName.Text = value; }
            public string Address
                get { return txtAddress.Text; }
                set { txtAddress.Text = value; }
            public string ContactNo
                get { return txtContact.Text; }
                set { txtContact.Text = value; }
            public string Message
                get { return ltrlMessage.Text; }
                set { ltrlMessage.Text = value; }
  5. Now it is time to write the model class. Before that, add a new Entity data model and name it and create a database connection accordingly. In the model class, write the following code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    namespace TestMVPApp.Model
    	public interface IModel
    		List<tbl_userdetails> ReturnDetails();
    	public class TestModel : IModel
    		private Entities textContext = new Entities();
    		private Tbl_UserDetails testDetails;
    		private IEnumerable<tbl_userdetails> detailsList;
    		public TestModel() { }
    		public bool SaveDetails(string firstName, 
    		string middleName, string lastName, string address, 
    						string contactNo)
    			testDetails = new Tbl_UserDetails()
    				FirstName = firstName,
    				MiddleName = middleName,
    				LastName = lastName,
    				Address = address,
    				ContactNo = contactNo
    			int testValue = textContext.SaveChanges();
    			if (testValue > 0)
    				return true;
    				return false;
    		public List<tbl_userdetails> ReturnDetails()
    			//detailsList = from v in 
    			//textContext.GetDetails() select v;
    			return detailsList.ToList<tbl_userdetails>();
  6. Now it is time to run the application, the following screen appears:


    Fig. main page(View)
  7. Fill in the details and submit the data. If the data is successfully saved in the database, then the following screen appears:


    Fig. Success screen


The article describes the ASP.NET MVP using Entity framework and guides you in a step by step manner. Happy coding.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Neerajan Lamsal
Software Developer
Nepal Nepal
I am a software developer with knowledge of C, C++, PHP, MySQL, C#.Net, ADO.Net, MSSQL Server, Crystal Reports, Asp.Net, Oracle, Asp.Net MVP, MVC, jQuery, javascript, WebService, WCF, Entity Framework and so on. I love to play on New Technologies.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 3 Pin
Vihang Shah15-May-13 7:41
memberVihang Shah15-May-13 7:41 
QuestionMVP with javascript Pin
Glenn197923-Oct-11 4:38
memberGlenn197923-Oct-11 4:38 

Do you have an example of an Model View Presenter pattern with a view that use jquery plugins (jqgrid,…)


Grtz Glenn
AnswerRe: MVP with javascript Pin
Neerajan Lamsal17-Nov-11 0:24
memberNeerajan Lamsal17-Nov-11 0:24 
QuestionDisplayViewPage.aspx does not exist in the attached zipped projectr Pin
Guttorm Haaversen30-Jul-11 15:08
memberGuttorm Haaversen30-Jul-11 15:08 
GeneralMy vote of 2 Pin
After205020-May-11 4:07
memberAfter205020-May-11 4:07 
GeneralRe: My vote of 2 Pin
Neerajan Lamsal10-Jun-11 3:33
memberNeerajan Lamsal10-Jun-11 3:33 
GeneralMy vote of 5 Pin
saxenaabhi614-May-11 1:32
membersaxenaabhi614-May-11 1:32 
GeneralRe: My vote of 5 Pin
Neerajan Lamsal10-Jun-11 3:31
memberNeerajan Lamsal10-Jun-11 3:31 
GeneralWhen I run the project in visual sudio 2010 Pin
Anil Srivastava8-May-11 4:12
memberAnil Srivastava8-May-11 4:12 
GeneralMy vote of 1 Pin
JV999921-Apr-11 3:37
memberJV999921-Apr-11 3:37 
GeneralRe: My vote of 1 Pin
Neerajan Lamsal22-Apr-11 2:02
memberNeerajan Lamsal22-Apr-11 2:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 14 May 2011
Article Copyright 2011 by Neerajan Lamsal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid