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>

 

转载于:https://www.cnblogs.com/shiliumu/p/8906963.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值