MVC架构+ef 添加、删除、查询用户。。。

首先

在Controller中修改Index方法,添加相关View, 显示所有用户

 public ActionResult Index()
        {
            return View(db.SysUsers);
        }

注意

Views 里面的Account到*.cshtml 顶部添加强类型声明(*代表名字)

@model IEnumerable<MVCDemo.Models.SysUser>

body中添加个table用来显示数据

<body>
    <div>
        <p>@Html.ActionLink("Create New","Create")</p>
        <table>
            <tr>
                <th>
                    @Html.DisplayNameFor(model=>model.UserName)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Email)
                </th>
                <th></th>
            </tr>
            @foreach (var item in Model)
            {
                 <tr>
                     <td>
                         @Html.DisplayFor(modelItem=>item.UserName)
                     </td>
                     <td>
                         @Html.DisplayFor(modelItem => item.Email)
                     </td>
                     <td>
                         @Html.ActionLink("Details", "Details", new{ id=item.ID})
                         @Html.ActionLink("Edit", "Edit", new { id = item.ID })
                         @Html.ActionLink("Delete", "Delete", new { id = item.ID })
                     </td>
                 </tr>
            }
        </table>
    </div>
</body>

 @Html.ActionLink("Details", "Details", new{ id=item.ID})
 @Html.ActionLink("Edit", "Edit", new { id = item.ID })
 @Html.ActionLink("Delete", "Delete", new { id = item.ID })

生成一个相同controller下的路由地址。

在Controller中增加相应的方法 Create  Edit Delete

然后分别添加相应的视图 

*.cshtml 的最顶端添加

@model MVCDemo.Models.SysUser

每个分别添加相应的代码

里面比较重要的  便于理解代码

DisplayNameFor (model=>model.xxx):生成纯文本,显示xxx列名

DisplayFor (model=>model.xxx):生成纯文本,显示xxx列的内容

LableFor:生成一个Lable标签

EditorFor : 生成一个text类型的input

PasswordFor : 类似于EditorFor, 隐藏文本内容

ActionLink :生成一个<a>标签

BeginForm : 生成一个表单

实现了简单的添加删除修改等、、、

 

posted @ 2017-10-22 21:30  你小子嚣张呀  阅读(319)  评论(0编辑  收藏  举报