asp.net MVC data Call sample code

Source: Internet
Author: User
(1) First we create an MVC project, of course, preferably the mvc1.0 version. (2) My demo, no reconfiguration routing resolution, using the default routing resolution address of the MVC project. Of course, if anyone is interested, you can add a default route yourself! (3) I have a database named Database1.mdf that contains a news form. (4) Then we right-click Models folder, Models "Add a new item, choose: Name can be free to start." I'm here called TEST.EDMX, and then click Add. Next: Here we can click on a new connection to select the database we want, the bottom is the webconfig stored in the connection string name, we can modify our own. Next: Select the database objects in the model here. then click Finish. In Solution Manager we can see the Models folder: So our database connection is complete. The following is how to use the database. (5) Below we add the Controller class file, the name is NewsController.cs I added the following method in Newscontroller: Public ActionResult List () {Testentities db = new Testentities ();//Instantiate the data object. var model = db. News.tolist ()//Call database in the News table return view (model);//Returns a model} (6) Then we right-click the list () and select Add View. Select Add. (7) So we are in the views file in see: More out of a news folder and list.aspx files. Open list.aspx file, in the first line we need to add: system.web.mvc.viewpage<ienumerable<mvcapplication1.models.news>> So we can get to the corresponding data model. In the view interface I am the following layout: List<table>
<tr>
<th>
Title
</th>
<th>
Author
</th>
</tr>
</table>
<%foreach (var item in Model)
{%>
<table>
<tr>
<td><a href= "/home/index/<%=html.encode" (item. news_id)%> "><%=html.encode" (item. News_title)%></a>
</td>
<td>
<%=html.encode (item. News_author)%>
</td>
</tr>
</table>
<%}%> I also added a link to the news title to get the news ID. (8) This enables us to use the database instance in the MVC project. Take a look at the effect: we click on the title: we jump to: the last parameter is the ID of the news. So we realize the display of database data.

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.