In this blog you will learn about Model View Presenter.
Separate the logic for the visual display from the event handling behaviour by putting them into two classes named as, the view and the presenter, respectively.
The view (the Web page or Web Part) manages the controls on the Web page and forwards user events to a presenter. The presenter contains the logic to respond to the events, updates the model (both the business logic and the application data), and alters the state of the view.
Step 1: Create an interface for a business object (Model). For example:
- public interface IModelStudent
- {
- IEnumerable<tbl_Student> StudentList();
- bool IsSuccessful { get; set; }
- void AddStudent(tbl_Student student);
- void EditStudent(tbl_Student student);
- tbl_Student GetStudent(long StuID);
- void DeleteStudent(long StuID);
-
- }
Step 2: Create a class for Model
- public class ModelStudent : IModelStudent
- {
- StudentDBEntities db;
-
- public ModelStudent()
- {
- db = new StudentDBEntities();
- }
-
- public IEnumerable<tbl_Student> StudentList()
- {
- return db.tbl_Student.ToList();
- }
- public bool IsSuccessful { get; set; }
- public void AddStudent(tbl_Student student)
- {
- try
- {
- db.tbl_Student.AddObject(student);
- db.SaveChanges();
- IsSuccessful = true;
- }
- catch (Exception)
- {
- IsSuccessful = false;
- }
- }
- public void EditStudent(tbl_Student student)
- {
- try
- {
- tbl_Student originalStudent = db.tbl_Student.Where(w => w.StuID == student.StuID).Select(s => s).FirstOrDefault();
- db.tbl_Student.ApplyCurrentValues(student);
- db.SaveChanges();
- IsSuccessful = true;
- }
- catch
- {
- IsSuccessful = false;
- }
- }
- public tbl_Student GetStudent(long StuID)
- {
- return db.tbl_Student.Where(w => w.StuID == StuID).Select(s => s).First();
- }
- public void DeleteStudent(long StuID)
- {
- try
- {
- var student = db.tbl_Student.Where(w => w.StuID == StuID).Select(s => s).FirstOrDefault();
- db.tbl_Student.DeleteObject(student);
- db.SaveChanges();
- IsSuccessful = true;
- }
- catch (Exception)
- {
- IsSuccessful = false;
- }
- }
-
- }
Step 3: Create an interface for View
- public interface IViewStudent
- {
- long StuID { get; set; }
- string StuName { get; set; }
- int Age { get; set; }
- IEnumerable<tbl_Student> students { get; set; }
- tbl_Student studentDetails { get; set; }
- event EventHandler LoadStudents;
- event EventHandler<EventArgs> AddStudent;
- event EventHandler<EventArgs> EditStudent;
- event EventHandler<EventArgs> GetStudent;
- event EventHandler<EventArgs> DeleteStudent;
- }
Step 4: Create UI like below with a Label, TextBox and Button.CRUD operation
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StudentIndo.aspx.cs" Inherits="MVPDemo.StudentIndo" %>
-
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="gvStudentList" runat="server" AutoGenerateColumns="false" ShowHeader="true">
- <Columns>
- <asp:TemplateField HeaderText="Student Name">
- <ItemTemplate>
- <%-- <input id="hndID" runat="server" type="hidden" value='<%# Eval("StuID") %>' />--%>
- <%# Eval("StuName") %>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Age">
- <ItemTemplate>
- <%# Eval("Age") %>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Edit">
- <ItemTemplate>
- <asp:LinkButton ID="lnkbtnEdit" runat="server" Text="Edit" OnClick="lnkbtnEdit_Click"
- CommandArgument='<%# Eval("StuID") %>'></asp:LinkButton>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Delete">
- <ItemTemplate>
- <asp:LinkButton ID="lnkbtnDelete" runat="server" Text="Delete" OnClick="lnkbtnDelete_Click"
- CommandArgument='<%# Eval("StuID") %>'></asp:LinkButton>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
- <br />
- <br />
- <table>
- <tr>
- <td>
- Stu Name :
- </td>
- <td>
- <input type="text" id="txtStuName" runat="server" placeholder="Enter Student Name"
- required="required" aria-describedby="name-format" aria-required="true" />
- </td>
- </tr>
- <tr>
- <td>
- Age :
- </td>
- <td>
- <input type="text" id="txtAge" runat="server" placeholder="Enter Age" required="required" aria-describedby="Age" aria-required="true" />
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
- </html>
Step 5: Create a Presenter class for collecting user inputs from View and pass view details to the Model.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using MVPDemo.Model;
- using MVPDemo.View;
- namespace MVPDemo.Presenter
- {
- public class PresenterStudent
- {
- IViewStudent PView;
- IModelStudent PModel;
- public bool IsSuccessful
- {
- get { return PModel.IsSuccessful; }
- }
- public PresenterStudent(IViewStudent IView)
- {
- PView = IView;
- PModel = new ModelStudent();
- Initialize();
- }
- private void Initialize()
- {
- PView.LoadStudents += mView_LoadData;
- PView.AddStudent += new EventHandler<EventArgs>(mView_Save);
- PView.EditStudent += new EventHandler<EventArgs>(mView_Edit);
- PView.GetStudent += new EventHandler<EventArgs>(mView_GetStudent);
- PView.DeleteStudent += new EventHandler<EventArgs>(mView_DeleteStudent);
- }
-
-
-
-
-
-
-
-
- private void mView_LoadData(object sender, EventArgs e)
- {
- PView.students = PModel.StudentList();
- }
-
- private void mView_Save(object sender, EventArgs e)
- {
- try
- {
- tbl_Student student = new tbl_Student();
- student.StuName = PView.StuName;
- student.Age = PView.Age;
- PModel.AddStudent(student);
- }
- catch (Exception)
- {
- }
- }
-
- private void mView_Edit(object sender, EventArgs e)
- {
- try
- {
- tbl_Student student = new tbl_Student();
- student.StuID = PView.StuID;
- student.StuName = PView.StuName;
- student.Age = PView.Age;
- PModel.EditStudent(student);
- }
- catch (Exception)
- {
- }
- }
-
- private void mView_GetStudent(object sender, EventArgs e)
- {
- try
- {
- PView.studentDetails = PModel.GetStudent(PView.StuID);
- }
- catch (Exception)
- {
- }
- }
- private void mView_DeleteStudent(object sender, EventArgs e)
- {
- try
- {
- PModel.DeleteStudent(PView.StuID);
- }
- catch (Exception)
- {
- }
- }
- }
- }
Step 6: Code-behind of ASPX page - View is communicating to the Model via Presenter
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using MVPDemo.Model;
- using MVPDemo.View;
- using MVPDemo.Presenter;
- namespace MVPDemo
- {
- public partial class StudentIndo : System.Web.UI.Page, IViewStudent
- {
- PresenterStudent PStudent;
- public long StuID { get; set; }
- public string StuName
- {
- get { return txtStuName.Value; }
- set { txtStuName.Value = value; }
- }
- public int Age
- {
- get { return Convert.ToInt32(txtAge.Value); }
- set { txtAge.Value = value.ToString(); }
- }
- public IEnumerable<tbl_Student> students { get; set; }
- public tbl_Student studentDetails { get; set; }
- public event EventHandler LoadStudents;
- public event EventHandler<EventArgs> AddStudent;
- public event EventHandler<EventArgs> EditStudent;
- public event EventHandler<EventArgs> GetStudent;
- public event EventHandler<EventArgs> DeleteStudent;
-
- public StudentIndo()
- {
- PStudent = new PresenterStudent(this);
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindData();
-
- }
- }
- private void clearControl()
- {
- txtStuName.Value = "";
- txtAge.Value = "";
- }
- private void BindData()
- {
- this.LoadStudents(this, new EventArgs());
- gvStudentList.DataSource = this.students;
- gvStudentList.DataBind();
- }
- protected void btnSave_Click(object sender, EventArgs e)
- {
- if (btnSave.Text == "Save")
- {
- if (this.AddStudent != null)
- {
- this.AddStudent(this, EventArgs.Empty);
- }
- }
- else
- {
- if (ViewState["StuID"] != null)
- {
- StuID = Convert.ToInt64(ViewState["StuID"]);
- }
- this.EditStudent(this, EventArgs.Empty);
- btnSave.Text = "Save";
- }
- BindData();
- clearControl();
- }
- protected void lnkbtnEdit_Click(object sender, EventArgs e)
- {
- LinkButton lnkbtn = sender as LinkButton;
- ViewState["StuID"] = StuID = Convert.ToInt64(lnkbtn.CommandArgument);
- this.GetStudent(this, EventArgs.Empty);
- StuName = this.studentDetails.StuName;
- Age = this.studentDetails.Age;
- btnSave.Text = "Update";
-
-
- }
- protected void lnkbtnDelete_Click(object sender, EventArgs e)
- {
- LinkButton lnkbtnDelete = sender as LinkButton;
- StuID = Convert.ToInt64(lnkbtnDelete.CommandArgument);
- this.DeleteStudent(this, EventArgs.Empty);
- BindData();
-
- }
- }
- }
Happy MVP Coding!!!