首先打開vs軟體
建立項目
建立web中的mvc項目
再右擊解決方案建立類庫項目
分别建立DAL層和BLL層再把DAL層和BLL層的類重命名
在mvc項目中的Models檔案夾建立model類
在DAL建立ADO.NET實體資料模型後把DAL層中App.Config檔案中的連結字元串複制
到mvc項目的Web.config檔案中
ADO.NET實體資料模型

DAL層中的類開始打代碼
/// <summary>
/// 兩表聯查
/// </summary>
/// <returns></returns>
public static List<dynamic> biao()
{
using (KaoshiEntities db = new KaoshiEntities())
{
var sql = from s in db.Student
join c in db.Bang on s.ID equals c.Bid
select new
{
s.Name,
s.passwork,
c.BName
};
List<dynamic> li = new List<dynamic>();
foreach (var item in sql.ToList())
{
dynamic d = new ExpandoObject();
d.name = item.Name;
d.pwd = item.passwork;
d.Bname = item.BName;
li.Add(d);
}
return li;
}
}
BLL層
/// <summary>
/// 兩表聯查
/// </summary>
/// <returns></returns>
public static List<dynamic> biao()
{
try
{
return KaoshiDAL.kaoshidal.biao();
}
catch (Exception ex)
{
throw ex;
}
}
mvc項目中的Models檔案夾的model類
/// <summary>
/// 兩表聯查
/// </summary>
/// <returns></returns>
public static List<dynamic> biao()
{
try
{
return KaoshiBLL.kaoshibll.biao();
}
catch (Exception ex)
{
throw ex;
}
}
在mvc項目中的Controllers檔案夾建立Home控制器
/// <summary>
/// 兩表聯查
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
List<dynamic> li =kaoshiModel.biao();
return View(li);
}
Index視圖
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table style="width: 40%;" >
<tr>
<th>姓名</th>
<th>密碼</th>
<th>班級</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.name</td>
<td>@item.pwd</td>
<td>@item.Bname</td>
</tr>
}
</table>