Implement MVC mode in ASP.NET (4)

zhaozj2021-02-16  48

Implement Model-View-Controller mode in ASP.NET (4)

Model - View - Reconstruction of Controller Separation

In order to solve the problems left above, you must separate the model to the controller role.

The implementation code of the view is the same as the front portion.

model

The following code example enables the model role to rely on the database without including any code that is dependent on the view.

Using system;

Using system.collections;

Using system.data;

Using system.data.sqlclient;

Public Class DatabaseGateway

{

Public Static DataSet GetRecordings ()

{

String selectcmd = "select * from recording";

SqlConnection myconnection =

NEW SQLCONNECTION

"Server = (local); database = recordings; trusted_connection = yes");

Sqldataadapter mycommand = new sqldataadapter (selectcmd, myconnection);

DataSet DS = New DataSet ();

MyCommand.Fill (DS, "Recording");

Return DS;

}

Public Static DataSet GetTracks (String RecordingID)

{

String selectcmd =

String.Format

"SELECT * from track where recordingId = {0} Order by id",

RecordingID);

SqlConnection myconnection =

NEW SQLCONNECTION

"Server = (local); database = recordings; trusted_connection = yes");

Sqldataadapter mycommand = new sqldataadapter (selectcmd, myconnection);

DataSet DS = New DataSet ();

MyCommand.fill (DS, "Track");

Return DS;

}

The current code only depends only on the database, this class is a channel of excellent database, which holds the SQL statement used to access the table or view, and other code calls some ways to complete the interaction with the database.

Controller

This reconstruction manner utilizes the code hidden mechanism, and the controller is responsible for the control of the event and method in the case where the model portion responsible for the data accesses is relatively independent. The task of the model is clear, it only returns a DataSet object. This implementation is like view code, and does not rely on how the data is returned from the database.

Using system;

Using system.data;

Using system.collections;

Using system.Web.ui.webcontrols;

Public Class Solution: System.Web.ui.page

{

Protected system.Web.ui.WebControls.button submit;

Protected system.Web.ui.WebControls.DataGrid mydatagrid;

Protected system.Web.ui.webcontrols.dropdownlist recordingselect;

Private void Page_load (Object Sender, System.Eventargs E) {

IF (! ispostback)

{

DataSet DS = DatabaseGateway.getRecordings ();

Recordingselect.datasource = ds;

Recordingselect.DataTextField = "Title";

Recordingselect.datavaluefield = "id";

Recordingselect.database ();

}

}

Void Submitbtn_Click (Object Sender, Eventargs E)

{

DataSet DS =

Databasegateway.gettracks

(String) RecordingSelect.SelectedItem.Value;

MyDataGrid.dataSource = DS;

MyDataGrid.databind ();

}

#Region Web Form Designer Generated Code

Override protected void oninit (Eventargs E)

{

//

// Codegen: This Call is Required by The ASP.NET Web Form Designer.

//

InitializationComponent ();

Base.onit (e);

}

///

/// Required Method for Designer Support - Do Not Modify

/// The contents of this method with the code editor.

///

Private vidinitiRizeComponent ()

{

This.Submit.Click = new system.eventhandler (this.submitbtn_click);

This.Load = New System.EventHandler (this.page_load);

}

#ndregion

}

转载请注明原文地址:https://www.9cbs.com/read-25075.html

New Post(0)