Heim >Datenbank >MySQL-Tutorial >MVC3+Linq to sql 显示数据库中数据表的数据

MVC3+Linq to sql 显示数据库中数据表的数据

WBOY
WBOYOriginal
2016-06-07 15:11:291016Durchsuche

1:首先创建asp.net mvc3应用程序 2:创建项目完成后 找到controllers文件鼠标右击选择添加控制器 3 为models文件夹添加一个linq to sql类文件,然后把数据库中的数据库复制进来。如截图操作 4:添加控制器好后会生成一个HomeController.cs类文件,其代码如

1:首先创建asp.net mvc3应用程序

MVC3+Linq to sql 显示数据库中数据表的数据

2:创建项目完成后 找到controllers文件鼠标右击选择添加控制器

MVC3+Linq to sql 显示数据库中数据表的数据

3 为models文件夹添加一个linq to sql类文件,然后把数据库中的数据库复制进来。如截图操作

MVC3+Linq to sql 显示数据库中数据表的数据

4:添加控制器好后会生成一个HomeController.cs类文件,其代码如下:

MVC3+Linq to sql 显示数据库中数据表的数据MVC3+Linq to sql 显示数据库中数据表的数据

<span>using</span><span> System;
</span><span>using</span><span> System.Collections.Generic;
</span><span>using</span><span> System.Linq;
</span><span>using</span><span> System.Web;
</span><span>using</span><span> System.Web.Mvc;
</span><span>using</span><span> MvcTestData.Models;
</span><span>namespace</span><span> MvcTestData.Controllers
{
    </span><span>public</span> <span>class</span><span> HomeController : Controller
    {
        </span><span>//</span>
        <span>//</span><span> GET: /Home/</span>

        <span>public</span><span> ActionResult Index()
        {
            TestDataContext txtData </span>= <span>new</span><span> TestDataContext();
            </span><span>var</span> result=<span>from</span> info <span>in</span><span> txtData.StuTable
                       </span><span>select</span><span> info;
            ViewData[</span><span>"</span><span>data</span><span>"</span>] =<span> result;
            </span><span>return</span><span> View(result);
        }

    }
}</span>
View Code

5 为HomeController.cs类文件中的Index()添加视图,其操作如下所示:

MVC3+Linq to sql 显示数据库中数据表的数据

6 添加视图完成后,其视图前台代码如下:

MVC3+Linq to sql 显示数据库中数据表的数据MVC3+Linq to sql 显示数据库中数据表的数据

<span>@using MvcTestData.Models
</span><span><span>DOCTYPE html</span><span>></span>
<span><span>html</span><span>></span>
<span><span>head</span><span>></span>
    <span><span>title</span><span>></span>Index<span></span><span>title</span><span>></span>
<span></span><span>head</span><span>></span>
<span><span>body</span><span>></span>
    <span><span>div</span><span>></span>
    <span><span>table </span><span>border</span><span>="0"</span><span> cellspacing</span><span>="0"</span><span> cellpadding</span><span>="0"</span><span> width</span><span>="100%"</span><span> style</span><span>="text-align:center"</span> <span>></span>
        <span><span>tr</span><span>></span>
            <span><span>th</span><span>></span>序号<span></span><span>th</span><span>><span>th</span><span>></span>学号<span></span><span>th</span><span>><span>th</span><span>></span>姓名<span></span><span>th</span><span>><span>th</span><span>></span>性别<span></span><span>th</span><span>><span>th</span><span>></span>年龄<span></span><span>th</span><span>><span>th</span><span>></span>住址<span></span><span>th</span><span>></span>
        <span></span><span>tr</span><span>></span><span>
@foreach (StuTable info in (ViewData["data"] as IEnumerable</span><span><span>StuTable</span><span>></span><span>))
{
   </span><span><span>tr</span><span>></span>
        <span><span>td</span><span>></span>@info.ID<span></span><span>td</span><span>></span>
        <span><span>td</span><span>></span>@info.StuId <span></span><span>td</span><span>></span>
        <span><span>td</span><span>></span>@info.StuName <span></span><span>td</span><span>></span>
        <span><span>td</span><span>></span>@info.StuSex <span></span><span>td</span><span>></span>
        <span><span>td</span><span>></span>@info.StuAge <span></span><span>td</span><span>></span>
        <span><span>td</span><span>></span>@info.StuAddress <span></span><span>td</span><span>></span>
   <span></span><span>tr</span><span>></span><span>
}
          </span><span></span><span>table</span><span>></span>
    <span></span><span>div</span><span>></span>
<span></span><span>body</span><span>></span>
<span></span><span>html</span><span>></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
View Code

7 最终测试运行结果

MVC3+Linq to sql 显示数据库中数据表的数据

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:(4) 数据库之 深入理解 单机事务Nächster Artikel:touch 命令