Click here to Skip to main content
11,707,254 members (57,954 online)
Click here to Skip to main content

Easy Example for Model View Presenter in ASP.NET - C#

, 22 Jan 2009 CPOL 92K 38
Rate this:
Please Sign up or sign in to vote.
It is a simple example for the implemention of Model View Presenter in Asp.Net

Model View Presenter

Model View Presenter is a software approach pattern conceived as derivative of Model View Controller.

What is Model?

Model is a domain level or business level object. A model accomodates application data and provides behaviours to systematically access it. It should be independent, it should not have any association with user interface issues. So we can reuse the same model for different type of UI level applications.

What is View?

A View is a windowpane for showing the model data or elements to the application users. Model does not have direct link to the views. View know about thier Models, but not the other way around.

What is Presenter?

Presenter will address the user input and use this to manipulate the model data. View will pass the user input actions to the Presenter for interpretation. Presenter will act on the received data from View and communicate with Model and produce results to the View.


Here is the classic example for implementing and understanding Model View Presenter pattern in an ASP.Net application. I believed this will helps you to get a good start in Model View Presenter.

Step 1: Create an interface for a business object (Model). For example:

public interface ICircleModel
{
    double getArea(double radius);
}

Step 2: Create a class for Model

public class CModel: ICircleModel
{
    public CModel(){}
    
    public double getArea(double radius)
    {
        return Math.PI * radius * radius;
    }
}

Step 3: Create an interface for View

public interface IView
{
    string RadiusText { get; set;}
    string ResultText { get; set;}
}

Step 4: Create UI like below with a Label, TextBox and Button. Textbox for getting radius of the circle, Label is for displaying the area and Button is for calculating the Area

<html xmlns="<a href="%22http://www.w3.org/1999/xhtml%22">http://www.w3.org/1999/xhtml</a>">
<head runat="server">
<title>MVP-Easy Example for ASP.Net C#</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<th colspan="2">Calculate Area of Circle</th>
</tr>
<tr>
<td>Enter Radius</td>
<td><asp:TextBox ID="TextRadius" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Result:</td>
<td><asp:Label ID="LabelResult" runat="server" ForeColor="red"></asp:Label></td>
</tr>
<tr align="right">
<td colspan="2"><asp:Button ID="ButtonResult" runat="server" Text="Get Area?" OnClick="ButtonResult_Click" /></td>
</tr>
</table>
</form>
</body>
</html>

Step 5: Create a Presenter class for collecting user inputs from View and pass view details to the Model.

public class CPresenter
{
    IView mview;
    public CPresenter(IView view)
    {
        mview = view;
    }
    public double CalculateCircleArea()
    {
        CModel model = new CModel();
        mview.ResultText = model.getArea(double.Parse(mview.RadiusText)).ToString();
        return mview.ResultText.ToString();
    }
}

Step 6: Code-behind of ASPX page - View is communicating to the Model via Presenter

public partial class _Default : System.Web.UI.Page,IView 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void ButtonResult_Click(object sender, EventArgs e)
    {
        CPresenter presenter = new CPresenter(this);
        presenter.CalculateCircleArea();
    }
    public string RadiusText
    {
        get{return TextRadius.Text;}
        set{TextRadius.Text = value;}
    }
    public string ResultText
    {
        get { return LabelResult.Text; }
        set { LabelResult.Text = value; }
    }
}

Now, I am sure, you are also feeling the same, it is pretty simple Smile | :)

Happy MVP Coding!!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

RB Changanacherry
Team Leader
India India
I am Rajesh Babu, currently working for Nous Infosystems, Bangalore. I have 6 plus years experience in Microsoft Technologies.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ashish Jain(Be Jovial)17-Aug-15 21:28
memberAshish Jain(Be Jovial)17-Aug-15 21:28 
GeneralThanks Pin
eng_int27-May-15 22:03
professionaleng_int27-May-15 22:03 
GeneralMy vote of 1 Pin
Alvin George27-Oct-14 19:49
memberAlvin George27-Oct-14 19:49 
QuestionCModel model = new CModel(); Pin
SayedAliTech22-Oct-14 3:27
memberSayedAliTech22-Oct-14 3:27 
GeneralVery nice! Pin
rkrockr11-Jun-14 18:12
grouprkrockr11-Jun-14 18:12 
Questionsimple any easy to understand Pin
siva02047-May-14 1:16
membersiva02047-May-14 1:16 
GeneralExcellent and easiest explaination..... Pin
Rajneesh Rai8-Apr-14 1:53
memberRajneesh Rai8-Apr-14 1:53 
QuestionExcellent example... Pin
Member 107307057-Apr-14 4:32
memberMember 107307057-Apr-14 4:32 
QuestionAbout MVP Pin
Member 980931029-Sep-13 22:51
memberMember 980931029-Sep-13 22:51 
GeneralMy vote of 5 Pin
manojmanirajan3-Jul-13 10:29
membermanojmanirajan3-Jul-13 10:29 
GeneralMy vote of 5 Pin
Mohamed hammad8-Feb-13 11:56
memberMohamed hammad8-Feb-13 11:56 
Questionnice Pin
kedarkeskar20-Aug-12 3:25
memberkedarkeskar20-Aug-12 3:25 
GeneralSimple and straight to the point Pin
kchannav9-May-12 19:37
memberkchannav9-May-12 19:37 
QuestionGood example but... Pin
bthompz22-Feb-12 13:43
memberbthompz22-Feb-12 13:43 
BugVery helpful Pin
Member 825899919-Oct-11 8:18
memberMember 825899919-Oct-11 8:18 
GeneralMy vote of 5 Pin
rj4530-Sep-11 7:07
memberrj4530-Sep-11 7:07 
GeneralMy vote of 5 Pin
sudhir kumar 223-Feb-11 22:10
membersudhir kumar 223-Feb-11 22:10 
GeneralMy vote of 5 Pin
David McGhee9-Nov-10 6:16
memberDavid McGhee9-Nov-10 6:16 
Generalgood intro Pin
Donsw22-Feb-09 6:40
memberDonsw22-Feb-09 6:40 
GeneralA very basic description of a MVP Passive View Pin
Ray Parker22-Jan-09 23:05
memberRay Parker22-Jan-09 23:05 
GeneralRe: A very basic description of a MVP Passive View Pin
Donsw9-Feb-09 2:36
memberDonsw9-Feb-09 2:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web01 | 2.8.150819.1 | Last Updated 23 Jan 2009
Article Copyright 2009 by RB Changanacherry
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid