.net mvc (a) will be extracted from the database to display the page

* Default has been connected database, the database entity name is: MusicStoreBD.cs *

A ##, a database is instantiated

① create a new controller MusicStore (optional) Controller in the project folder in
② instantiated: MusicStoreBD ms = new MusicStoreBD () ;

MusicStoreBD ms = new MusicStoreBD();

## Second, add operation
① extract data
② Display data
`` `CSharp
public ActionResult Index ()
{
var musiclist from I = I in ms.MusicInfo SELECT;
// LinQ statement, extract data from the database
// MusicInfo is a table
return View (musiclist.ToList ());
// perform ToList () operation, a listing
}
`` `
NOTE: MusicStore complete code controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MusicStore.Models;
namespace MusicStore.Controllers
{
public class StoreController : Controller
{
// GET: Store
MusicStoreBD ms = new MusicStoreBD();
public ActionResult Index()
{
var musiclist = from i in ms.MusicInfo select i;
//LinQ语句,从数据库中提取数据
//MusicInfo is a table 
return View (musiclist.ToList ());
 // perform ToList () operation, a listing 
} 
} 
}

 

Third, add a view
① Right-click: Index (), select "Add View" option

This is the view automatically added back to the Index view
that we add the following data for the page.
Fourth, the display data inside the database
we just use ToList () method, the musiclist cast list was established, we will use mysiclist download to display the data.
① strongly typed view, the conversion table can be enumerated, that is, one data is displayed.

IEnumerable @model <MusicStore.Models.MusicInfo>

② the data shown
here we use a foreach loop.
Example: The MusicStore the MusicID, i.e., numbers are displayed
Code:

How to see results? Right-click on: View in the browser or CTRL + shift + W
effect is as follows:



③ complete data is extracted:

@model IEnumerable<MusicStore.Models.MusicInfo>
@{
    ViewBag.Title = "Index";
}

<h1>我的音乐情况</h1><br /><br/>

<table class="table">
    <tr>
        <th>编号</th>
        <th>名称</th>
        <th>时间</th>
        <th>价格</th>
        <th>评级</th>
    </tr>
    <tbody id="userlist">
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @item.MusicID
                </td>
                <td>
                    @item.MusicName
                </td>
                <td>
                    @item.MusicCreateTime
                </td>
                <td>
                    @item.MusicPrice
                </td>
                <td>
                    @item.MusicLevel
                </td>
            </tr>
        }
    </tbody>
</table>

 

效果如下:

Guess you like

Origin www.cnblogs.com/yuexiliuli/p/11622021.html