Implementing the MVC pattern in asp.net (iv.)

Source: Internet
Author: User
asp.net implementation of Model-view-controller mode in asp.net (iv.)

Reconstruction of Model-view-controller separation

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

The implementation code for the view is the same as in the previous section.

Model

The following code example makes the model role depend solely on the database and does not contain 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);d atabase=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);d atabase=recordings; Trusted_connection=yes ");

SqlDataAdapter mycommand = new SqlDataAdapter (Selectcmd, MyConnection);



DataSet ds = new DataSet ();

Mycommand.fill (ds, "Track");

return DS;

}



Now the code relies only on the database, which is a good database channel that holds the SQL statements that access the table or view, and other code calls methods to complete the interaction with the database.

Controller

This reconfiguration method utilizes the code-hiding mechanism, in which the Controller is responsible for the control of the event and method in the case that the model part of the data access is relatively independent. The task of the model is clear, and it returns only a DataSet object. This implementation, like view code, does not depend 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.databind ();

}

}



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 the call are required by the ASP.net Web Form Designer.

//

InitializeComponent ();

Base. OnInit (e);

}



<summary>

Required to Designer support-do not modify

The contents is with the Code Editor.

</summary>

private void InitializeComponent ()

{

This.submit.Click + = new System.EventHandler (this. SubmitBtn_Click);

This. Load + = new System.EventHandler (this. Page_Load);



}

#endregion

}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.