1:首先创建asp.net mvc3应用程序 2:创建项目完成后 找到controllers文件鼠标右击选择添加控制器 3 为models文件夹添加一个linq to sql类文件,然后把数据库中的数据库复制进来。如截图操作 4:添加控制器好后会生成一个HomeController.cs类文件,其代码如
1:首先创建asp.net mvc3应用程序
2:创建项目完成后 找到controllers文件鼠标右击选择添加控制器
3 为models文件夹添加一个linq to sql类文件,然后把数据库中的数据库复制进来。如截图操作
4:添加控制器好后会生成一个HomeController.cs类文件,其代码如下:
<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>
5 为HomeController.cs类文件中的Index()添加视图,其操作如下所示:
6 添加视图完成后,其视图前台代码如下:
<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>
7 最终测试运行结果