> 데이터 베이스 > 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으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿