zoukankan      html  css  js  c++  java
  • 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 : 生成一个表单

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

  • 相关阅读:
    Java变量相关
    centos 7安装Docker
    字符串匹配的KMP算法
    [转]关于”算法工程师/机器学习工程师”的笔试和面试总结
    希腊字母与拉丁字母的对应
    决策树和基于决策树的集成方法(DT,RF,GBDT,XGB)复习总结
    机器学习总结
    bat批处理的注释语句
    Python中使用pickle持久化对象
    怎样用通俗的语言解释REST,以及RESTful?
  • 原文地址:https://www.cnblogs.com/wwr01/p/7711966.html
Copyright © 2011-2022 走看看