先将源代码贴上,UserInfo 模型层
using System;
using System.Data.Entity;
namespace Mvc.Models
{
public class UserInfo
{
public int UserID { get; set; }
public string UserName { get; set; }
public bool UserSex { get; set; }
public DateTime UserDate { get; set; }
}
public class UserDBcontext : DbContext {
public DbSet<UserInfo> DbTest { get; set; }
}
}
控制层
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvc.Models;
namespace Mvc.Controllers
{
public class UserInfoController : Controller
{
//
// GET: /Use/
UserDBcontext us = new UserDBcontext();
public ActionResult Index()
{
var skk = from m in us.DbTest where m.UserID < 100 select m;
return View(skk.ToList());
}
}
}
wiew 显示层
@model IEnumerable<Mvc.Models.UserInfo>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
UserID
</th>
<th>
UserName
</th>
<th>
UserSex
</th>
<th>
UserDate
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserID)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserSex)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserDate)
</td>
<td>
</td>
</tr>
}
</table>
web.config 文件
<configuration>
<connectionStrings>
<add name="DbTest"
connectionString=" Data Source=.;
Initial Catalog=DbTest;Persist Security Info=True;
User ID=sa;Password=1s@u@n* "
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="1.0.0.0"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />