MVC with Asp.net
| Model View Controller : MVC with Asp.net step by step implementation |
| "Models" in a MVC based application are the components of the application that are responsible for maintaining state. Often this state is persisted inside a database (for example: we might have a Product class that is used to represent order data from the Products table inside SQL). |
 | Model code sample |
| using System;
| | 1:using System.Collections.Generic;
| | 2:using System.Text;
| | 3:using BusinessEntities;
| | 4:
| | 5:namespace MVC.Module
| | 6:{
| | 7: public interface IEmployeeModel
| | 8: {
| | 9: bool AddEmployee(Employee emp);
| | 10: Employee GetEmployee(int EmpId);
| | 11:
| | 12: }
| | 13:
| | 14:
| | 15:
| | 16: public class EmployeeModel : IEmployeeModel
| | 17: {
| | 18: public delegate void EmployeeAddedHandler(object sender, EmployeeAddedEventArgs e);
| | 19: public event EmployeeAddedHandler EmployeeAdded;
| | 20:
| | 21:
| | 22: public EmployeeModel()
| | 23: {
| | 24:
| | 25: }
| | 26:
| | 27: public bool AddEmployee(Employee emp)
| | 28: {
| | 29: // call DAL Layer here. add employee to database.
| | 30:
| | 31: return true;
| | 32: }
| | 33:
| | 34: public Employee GetEmployee(int EmpId)
| | 35: {
| | 36: Employee emp = null;
| | 37:
| | 38: // call DAL Layer here. get employee info from databse.
| | 39:
| | 40:
| | 41:
| | 42:
| | 43: return emp;
| | 44:
| | 45: }
| | 46:
| | 47:
| | 48: protected void onEmployeeAdded()
| | 49: {
| | 50: // EmployeeAdded(this,
| | 51: }
| | 52:
| | 53: }
| | 54:
| | 55:
| | 56: public class EmployeeAddedEventArgs : System.EventArgs
| | 57: {
| | 58: private bool _isAdded = false;
| | 59:
| | 60: public EmployeeAddedEventArgs(object isAdded)
| | 61: {
| | 62: _isAdded = (bool)isAdded;
| | 63: }
| | 64:
| | 65: //public
| | 66:
| | 67: }
| | 68:}
| | 69: |
|
|
View : This layer all about presentation layer, simply inherit IView interface and implement the methods, thats the beauty of MVC design patteren, IView is always updated by IController. |
 | View code sample |
| /*
| | 1: * This layer will get data from MVC.Model,
| | 2: * and will be controlled by MVC.Controller
| | 3: *
| | 4:*/
| | 5:
| | 6:using System;
| | 7:using System.Collections.Generic;
| | 8:using System.Text;
| | 9:using BusinessEntities;
| | 10:
| | 11:namespace MVC.View
| | 12:{
| | 13: public interface IEmployeeView
| | 14: {
| | 15: Employee GetEmployee();
| | 16:
| | 17: void AddEmployee();
| | 18: }
| | 19:}
| | 20: |
|
|
"Controllers " in a MVC based application are the components responsible for handling end user interaction, manipulating the model, and ultimately choosing a view to render to display UI. In a MVC application the view is only about displaying information - it is the controller that handles and responds to user input and interaction. |
 | Controller code sample |
| using System;
| | 1:using System.Collections.Generic;
| | 2:using System.Text;
| | 3:using BusinessEntities;
| | 4:using MVC.Module;
| | 5:
| | 6:namespace MVC.Controller
| | 7:{
| | 8:
| | 9: public interface IEmployeeController
| | 10: {
| | 11: bool AddEmployee(Employee emp);
| | 12: Employee GetEmployee(int EmpId);
| | 13: List<Employee> GetEmployees();
| | 14: }
| | 15:
| | 16:
| | 17:
| | 18: public class EmployeeController : IEmployeeController
| | 19: {
| | 20: private IEmployeeView _view;
| | 21: private IEmployeeModel _empModel;
| | 22:
| | 23: public EmployeeController()
| | 24: {
| | 25: this._view = view;
| | 26: this._empModel = new EmployeeModel();
| | 27: }
| | 28:
| | 29:
| | 30: public bool AddEmployee(Employee emp)
| | 31: {
| | 32: bool _isAdded = _empModel.AddEmployee(emp);
| | 33:
| | 34: _view.AddEmployee();
| | 35:
| | 36: return _isAdded;
| | 37:
| | 38: }
| | 39:
| | 40:
| | 41:
| | 42: public Employee GetEmployee(int EmpId)
| | 43: {
| | 44:
| | 45: return _empModel.GetEmployee(EmpId);
| | 46: }
| | 47:
| | 48: public List<Employee> GetEmployees()
| | 49: {
| | 50:
| | 51: return _empModel.GetEmployees();
| | 52: }
| | 53:
| | 54:
| | 55: }
| | 56:}
| | 57: |
|
|
"Implementation " Now look at the MVC implementation with asp.net page . |
 | implementation with asp.net page |
| using System;
| | 1:using System.Data;
| | 2:using System.Configuration;
| | 3:using System.Linq;
| | 4:using System.Web;
| | 5:using System.Web.Security;
| | 6:using System.Web.UI;
| | 7:using System.Web.UI.HtmlControls;
| | 8:using System.Web.UI.WebControls;
| | 9:using System.Web.UI.WebControls.WebParts;
| | 10:using System.Xml.Linq;
| | 11:
| | 12:namespace MVCImplementation
| | 13:{
| | 14: public partial class MVCSample : System.Web.UI.Page, MVC.View.IEmployeeView
| | 15: {
| | 16: MVC.Controller.IEmployeeController _controller;
| | 17:
| | 18: protected void Page_Load(object sender, EventArgs e)
| | 19: {
| | 20: _controller = new MVC.Controller.EmployeeController(this);
| | 21: }
| | 22:
| | 23:
| | 24:
| | 25:
| | 26: #region IEmployeeView Members
| | 27:
| | 28: public Employee GetEmployee()
| | 29: {
| | 30: throw new NotImplementedException();
| | 31: }
| | 32:
| | 33:
| | 34:
| | 35: public void AddEmployee()
| | 36: {
| | 37: BusinessEntities.Employee emp = new Employee();
| | 38: emp.Name = txtName.Text;
| | 39: emp.Address = txtAddress.Text;
| | 40: emp.Email = txtEmail.Text;
| | 41:
| | 42: this._controller.AddEmployee(emp);
| | 43: }
| | 44:
| | 45:
| | 46: #endregion
| | 47:
| | 48: protected void btnAdd_Click(object sender, EventArgs e)
| | 49: {
| | 50: AddEmployee();
| | 51: }
| | 52:
| | 53: protected void btnGetDetails_Click(object sender, EventArgs e)
| | 54: {
| | 55: gvDetails.DataSource = this._controller.GetEmployees();
| | 56: gvDetails.DataBind();
| | 57: }
| | 58: }
| | 59:}
| | 60: |
|
|
Asp.net, Ado.net, .Net Remoting, .Net
Webservice, SQL, XML, XSLT, WCF, WPF, WWF NHibernate, Ajax, Jquery, DHTML