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>
  • 相关阅读:
    Linux数据备份
    eclipse 中使用等宽字体 inconsolata
    在ubuntu14.04 64位中使用jd-gui
    Fragment 常见问题
    ClassNotFoundException
    符号表的简单使用
    一个简单的词法分析器
    一个简单的语法分析器(后缀式转换)
    火狐无法显示图片
    ftp 匿名访问设置
  • 原文地址:https://www.cnblogs.com/shiliumu/p/8906963.html
Copyright © 2011-2022 走看看