This exampleProgramDemonstrate how to use Microsoft ASP. Net MVC preview 5 and LINQ to SQL to develop Web applications.
1. ASP. NET MVCIntroduction
The ASP. net mvc framework is another development method after ASP. NET webforms. It provides a series of excellent features that enable ASP. NET developers to have another choice. MVC is a framework technology that divides the implementation of an application into three component roles: model, view, and controller.
In MVC-based applications, model is an application component that maintains the state. This status usually persists in the database.
In MVC-based applications, view is a component used to display user interfaces. This UI is usually created using model data.
In MVC-based applications, the controller is used to process user interaction, operate the model, and select which view to use to display the UI components. In MVC applications, views are only used to display information. They are used by controllers to process and respond to user input and interaction.
One advantage of using the MVC method is that it helps to promote the clear separation of attention between models, views, and controllers in applications. Keeping a clear separation of focus makes it extremely easy to test applications, because the definition and Expression of contracts between different application components are clearer.
2. MVCAnd LINQ to SQLDemo program
Below is a simple sample program designed by the entlib.com Forum Group (http://forum.EntLib.com) for the latest Microsoft ASP. net mvc Framework (Preview 5. The example program is based on the entlib.com Forum database demonstration, which can be easily switched to other databases.
The following is the running effect interface of the sample program:
3.Build a SQL-basedModel
The following is the sample project source code:
The "model" section is created using LINQ to SQL. The sample program uses the entlib.com Forum database and adds four tables. The operation interface is as follows:
4.Create entlibforumcontrollerController
The entlibforumcontroller class first adds a previously created model-entlibforumdatacontext, and then compiles methods for retrieving Forum, topic, and post content. DetailsCodeAs shown in the following figure:
Public class entlibforumcontroller: Controller
{
Private entlibforumdatacontext m_forum = new entlibforumdatacontext ();
Public actionresult index ()
{
// Add action logic here
Throw new notimplementedexception ();
}
Public actionresult forums ()
{
Viewdata ["Forums"] = datacontext. yaf_forums.tolist ();
Return view ();
}
Public actionresult topics (int id)
{
Viewdata ["topics"] = datacontext. yaf_topics.where (A => A. forumid = ID). tolist ();
Return view ();
}
Public actionresult messages (int id)
{
Viewdata ["messages"] = datacontext. yaf_messages.where (A => A. topicid = ID). tolist ();
Return view ();
}
# Region Properties
Private entlibforumdatacontext datacontext
{
Get {return m_forum ;}
}
# Endregion
}
5.Create UIView
Creating a UI view is relatively simple. When creating a UI view, select MVC View content page and master page select the site. master file under the shared directory.
The following is the UI and code of the forums. ASPX page:
Public partial class forums: viewpage
{
Protected void page_load (Object sender, eventargs E)
{
Grdforum. datasource = (list <yaf_forum>) viewdata ["Forums"];
Grdforum. databind ();
}
}
Welcome to the http://forum.entlib.com open source ASP. NET forum for more. netTechnical materialsAnd system architecture design documents, more technical experts knowledge sharing.
Attachment:
Mvc_demo_entlib.zip548 kb, 1,624 downloads.