工具:VS.net201三、EF六、MVC五、SQLServer2008html
參考出處:數據庫
http://www.cnblogs.com/slark/p/mvc-5-get-started-create-project.html瀏覽器
http://www.cnblogs.com/miro/p/4288184.htmlmvc
http://www.cnblogs.com/dotnetmvc/p/3732029.html框架
在SqlServer上建立數據庫:Element工具
模擬兩個表並插入數據:SysUser(用戶表)、SysRole(角色表)佈局
CREATE TABLE [dbo].[SysUser](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nchar](10) NOT NULL,
[RoleNum] [nchar](10) NOT NULL
) ON [PRIMARY]post
CREATE TABLE [dbo].[SysRole](
[ID] [int] IDENTITY(1,1) NOT NULL,
[RoleName] [nchar](10) NOT NULL,
[RoleNum] [nchar](10) NOT NULL
) ON [PRIMARY]學習
插入數據:url
在Controllers文件夾上右鍵--添加--控制器
namespace MVCDemo.ViewModels
{
public class UserRole
{
public string userName { get; set; }
public string userRole { get; set; }
}
}
右鍵Controllers文件夾添加控制類,此類繼承於Controller類
using System;
using System.Collections.Generic;
using System.Linq; using System.Web;
using System.Web.Mvc; using System.Data.Entity;
using MVCDemo.ViewModels;
using MVCDemo.Models;
namespace MVCDemo.Controllers
{
public class UserRoleController : Controller
{
ElementModel db = new ElementModel();
public ActionResult Index()
{
var userRoleList = from uu in db.SysUsers
join ud in db.SysRoles on uu.RoleNum equals ud.RoleNum
where uu.ID == 1
select new UserRole {userName = uu.Name,userRole = ud.RoleName}
return View(userRoleList);
}
}
}
@model IEnumerable<MVCDemo.ViewModels.UserRole>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model=>model.userName)
</th>
<th>
@Html.DisplayNameFor(model => model.userRole)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.userName)
</td>
<td>
@Html.DisplayFor(modelItem => item.userRole)
</td>
</tr>
}
</table>