首頁 > 資料庫 > mysql教程 > MVC3+Linq to sql 显示数据库中数据表的数据

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

WBOY
發布: 2016-06-07 15:11:29
原創
1016 人瀏覽過

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 显示数据库中数据表的数据

 

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板