Dynamicdata for Asp.net MVC message. This example provides the preparation work and displays a list of articles.

Source: Internet
Author: User

Dynamicdatamvc has been around for a while and has never been ready to face it. It seems nice to take some time to understand it today.

An example of a blog is provided during download, but the example is a little complicated and it is not easy to clarify the concept.

Appendix

Download and create a project

: Http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx to select the dynamicdatamvc.zip on the line, such as with the version changes, please give an inverse three.

The environment in this article is Visual Studio 2008/SQL Server 2005

1. Create an Asp.net MVC project: dmvc

2. Reference Microsoft. Web. dynamicdata. MVC. dll and system components system. componentmodel. dataannotations and System. Web. dynamicdata

3. Add an add node to pages/namespaces in Web. config. The namespace attribute of the add node is Microsoft. Web. dynamicdata. MVC.

4. Add the node add <add Assembly = "system. Web. dynamicdata, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35"/> in system. Web/compilation/assemblies.

5. Set views/shared/In dynamicdatamvcValidationsummary. ascxCopy entitytemplates and fieldtemplates to the views/shared folder of the newly created MVC project.

 

Database preparation

Create an articles table

Open Microsoft SQL Server Management studio and right-click the database to create a database.

Generate the dbml file of LINQ to SQL:

If this parameter is not set, you can search for the Keyword: "LINQ to SQL ".

This datacontext is called dmvcdatadatacontext

For the convenience of query, we first add some data to it (20 will be enough if we don't write much)

  Insert into [articles] ([title], [body], [addtime], [author]) values ('Article1','Content 1', Getdate (),'Classic') Insert into [articles] ([title], [body], [addtime], [author]) values ('Article 2','Content 2', Getdate (),'Classic') Insert into [articles] ([title], [body], [addtime], [author]) values ('Article 3','Content 3', Getdate (),'Classic')
Let the website run and display the article list

Come First (I am doing this for the artist. You may understand)

This is to list the article. We will try our best to achieve this goal.

First, it is also very important to add the datacontext to be automated in application_start.

 
  Protected VoidApplication_start () {registerroutes (routetable. routes); VAR model =NewMetamodel (); Model. registercontext (Typeof(Dmvc. Models. dmvcdatadatacontext ),NewContextconfiguration {scaffoldalltables =True});}

If you do not know where application_start is, you can use the global. asax file.

We will create a new Controller: articlecontroller to specifically implement the crud (Create/read/update/delete) of article ).

First, create an action called list. read data from the database and put it in viewdata. model.

      Public   Class Articlecontroller: controller { /// <Summary>          /// Document list          /// </Summary>          /// <Param name = "p"> current page number </param>          /// <Param name = "Ps"> pagesize </param>          /// <Returns> </returns>          Public Actionresult list ( Int ? P, Int ? PS ){//              If (! P. hasvalue) p = 1; // When the processing is empty              If (! PS. hasvalue) PS = 4; // Four entries per page by default              Using (Var db = New Dmvcdatadatacontext () {var x = New Pagedlist <articles> (db. Articles, P. Value, PS. value ); // Read data and automatically pagination                  Return View (x );}}}

Here we use the pagedlist class to automatically paging dB. articles. It is an auxiliary tool for dynamicdata and is quite good at using it.

Create a view page to inherit from pagedlist <articles>

That is:

 
  PublicPartialClassList: viewpage <pagedlist <articles> {}

In view, we write the following:Code:

  <! -- Display list start -->  <% Foreach (var a in viewdata. Model ){ %>  <  Fieldset  > <  Legend  >  <% Html. renderdynamicfield (a, "title "); %>  </  Legend  > Content: <% Html. renderdynamicfield (a, "body "); %>  <  BR   /> Posting Date:<% Html. renderdynamicfield (a, "addtime "); %> Author: <% Html. renderdynamicfield (a, "author "); %>  </  Fieldset  >  <% } %>  <! -- Display the end of the list -->  <! -- Start with the paging button --> Paging button<% If (viewdata. model. haspreviouspage ){ %>  <% = Html. actionlink ("first page", "list", new {p = 1}, null) %>  <% = Html. actionlink ("Previous Page", "list", new {P = viewdata. model. currentpage-1}, null) %>  <% } Else { %> Previous Page <% }%> Current: <% = Viewdata. model. currentpage %> /Total <% = Viewdata. model. totalpages %> Page <% If (viewdata. model. hasnextpage ){ %>  <% = Html. actionlink ("next page", "list", new {P = viewdata. model. currentpage + 1}, null) %> <% = Html. actionlink ("last page", "list", new {P = viewdata. model. totalpages}, null) %>  <% } Else { %> Next Page <% } %>  <! -- The button for paging ends --> 

 

Although it is a bit messy, it is basically divided into two parts: data display and paging button. The last access is/article/list? P = 2. The result shown in the preceding figure is displayed.

 

Example download: http://files.cnblogs.com/chsword/DMvc.rar

 

Dynamicdata for Asp.net MVC message this example is updated later

Dynamicdata for Asp.net MVC message this instance creates. deletes. Data Verification

Dynamicdata for Asp.net MVC message. This example provides the preparation work and displays a list of articles.

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.