zoukankan      html  css  js  c++  java
  • 5、使用EF对后台SysSample数据增删改查

    一、新建SysSampleMVC控制器,并生成view视图,view视图使用布局页

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace MvcApp.Controllers
    {
        public class SysSampleMVCController : Controller
        {
            //
            // GET: /SysSampleMVC/
    
            public ActionResult Index()
            {
                return View();
            }
        }
    }

    生成的Index.cshtml如下图

    F5运行,点击“菜单一”,出现下面界面

    二、使用EF查询

    1、添加 using MvcApp.Models;

         在SysSampleMVC控制器下增加:DBContainer db = new DBContainer();

    2、修改Index方法

            public ActionResult Index()
            {
                IQueryable<SysSample> list = db.SysSample.AsQueryable();
                return View(list);
            }

    3、修改Index.cshtml

    顶部添加强类型声明 @model IEnumerable<MvcApp.Models.SysSample>

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

    @model IEnumerable<MvcApp.Models.SysSample>
    
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Index_Layout.cshtml";
    }
    
    <body>
        <p>
            @Html.ActionLink("Create New", "Create")
        </p>
        <table class="table table-condensed">
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Id)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Name)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Age)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Bir)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Photo)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Note)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.CreateTime)
                </th>
            </tr>
    
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Id)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Name)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Age)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Bir)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Photo)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Note)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.CreateTime)
                    </td>
                    <td>
                        @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                        @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                        @Html.ActionLink("Delete", "Delete", new { id = item.Id })
                    </td>
                </tr>
            }
        </table>
    </body>
  • 相关阅读:
    springboot开发之配置Servlet三大组件(Servlet、Filter、Listener)
    springboot开发之配置嵌入式Servlet容器两种方式
    springboot开发之利用idea自带的插件模拟客户端请求
    springboot开发之配置自定义的错误界面和错误信息
    springboot开发之删除员工
    springboot开发之修改员工
    springboot开发之添加员工
    springboot开发之thymeleaf页面公共元素的抽取
    oracle 创建表、删除表、添加字段、删除字段、表备注、字段备注、修改表属性
    C#拼装JSON数组简易方法
  • 原文地址:https://www.cnblogs.com/shiliumu/p/8906963.html
Copyright © 2011-2022 走看看