熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET MVC 4框架揭秘:MVP(3)[1]

2022-06-13   來源: .NET編程 

    MVP(

  作為MVP三角關系核心的Presenter通過EmployeeSearchPresenter表示如下面的代碼片段所示表示View的只讀屬性類型為IEmployeeSearchView接口而另一個只讀屬性Repository則表示作為Model的EmployeeRepository對象兩個屬性均在構造函數中初始化

  public class EmployeeSearchPresenter

  {

  public IemployeeSearchView View { get; private set; }

  public EmployeeRepository   Repository { get; private set; }

  public EmployeeSearchPresenter(IEmployeeSearchView view)

  {

  thisView                       = view;

  thisRepository                 = new EmployeeRepository()

  thisViewDepartmentSelected    += OnDepartmentSelected;

  }

  public void Initialize()

  {

  IEnumerable<Employee> employees = thisRepositoryGetEmployees()

  thisViewBindEmployees(employees)

  string[] departments =

  new string[] { 銷售部 采購部 人事部 IT部 };

  thisViewBindDepartments(departments)

  }

  protected void OnDepartmentSelected(object sender

  DepartmentSelectedEventArgs args)

  {

  string department       = argsDepartment;

  var employees           = thisRepositoryGetEmployees(department)

  thisViewBindEmployees(employees)

  }

  }

  在構造函數中我們注冊了View的DepartmentSelected事件作為事件處理器的OnDepartmentSelected方法通過調用Repository(即Model)得到了用戶選擇部門下的員工列表返回的員工列表通過調用View的BindEmployees方法實現了在View上的數據綁定在Initialize方法中我們通過調用Repository獲取所有員工的列表並通過View的BindEmployees方法顯示在界面上作為篩選條件的部門列表通過調用View的BindDepartments方法綁定在View上

  最後我們來看看作為View的Web頁面如何定義如下所示的是作為頁面主體部分的HTML核心部分是一個用於綁定篩選部門列表的DropDownList和一個綁定員工列表的GridView

  <html xmlns=http://wwwworg//xhtml>

  <head>

  <title>員工管理</title>

  <link rel=stylesheet href=Stylecss />

  </head>

  <body>

  <form id=form runat=server>

  <div id=page>

  <div class=top>

  選擇查詢部門

  <asp:DropDownList ID=DropDownListDepartments

  runat=server />

  <asp:Button ID=ButtonSearch runat=server Text=查詢

  OnClick=ButtonSearch_Click />

  </div>

  <asp:GridView ID=GridViewEmployees runat=server

  AutoGenerateColumns=false Width=%>

[]  []  


From:http://tw.wingwit.com/Article/program/net/201311/16124.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.