使用.NET framework MVC编写简易的增删查

一,页面效果

二,前期准备

           1,数据库创建好表

           2,新建项目

           3,添加数据实体模型 

三,代码实现

        一,页面效果

1,首页效果

2,新增页面

3.删除页面

4,查询页面

二, 前期准备

 1,数据库创建好表

设置好主外键,主键,自动增长列

 2,创建项目

打开visual studio 2019,选择项目模板 选择.NET Framework项目

点击“下一步”

 

  3,添加数据实体模型

三,代码实现

1,在Controllers文件下添加一个.cs文件

获取表数据,左连接,使用Include链接数据

public class TestController : Controller
    {
        // GET: Test
        TestEntities1 dbContext=new TestEntities1();
        public ActionResult Index()
        {
            //左链接,使用Include链接数据
            var list = dbContext.Schedule.Include("Type");
            return View(list.ToList());
        }

 添加index视图添加表格数据代码

<table border="1">
    <thead>
        <tr>
            <td>日程编号</td>
            <td>日程执行人</td>
            <td>日程分类</td>
            <td>计划开始时间</td>
            <td>计划结束时间</td>
            <td>日程内容</td>
            <td>备注</td>
        </tr>

        @{
            foreach (var item in Model)
            {
                <tr>
                    <td>@item.ID</td>
                    <td>@item.Name</td>
                    <td>@item.Type.Name</td>
                    <td>@item.BeginTime</td>
                    <td>@item.EndTime</td>
                    <td>@item.Content</td>
                    <td>@item.Remark</td>
                    <td><a>删除</a></td>
                </tr>
            }


        }
    </thead>
</table>

获得表格显示的效果

(1) 删除实现的代码

返回到.cs文件编写方法

public ActionResult DoAdd(Schedule schedule)
        {
            dbContext.Schedule.Add(schedule);
            dbContext.SaveChanges();
            return Redirect("index");
                }

到视图添加路径

<td><a href="/test/Delete?id=@item.ID" onclick="return confirm('确认删除吗')">删除</a></td>

(2)查询代码

到.cs文件添加代码 

 public ActionResult Index(int? TypeID,string Name)
        {
            //左链接,使用Include链接数据
            var list = dbContext.Schedule.Include("Type");
            if(TypeID!=null)
            {
                list =(DbQuery<Schedule>)  list.Where(m => m.TypeID == TypeID);
            }
            if (!string.IsNullOrEmpty(Name))
                    {
                list = (DbQuery<Schedule>)list.Where(m => m.Name.Contains("Name"));
            }
            var list2 = dbContext.Type.ToList();
            ViewBag.TypeID = new SelectList(list2, "ID", "Name");
            return View(list.ToList());
        }

index视图代码

<form>
    @Html.DropDownList("TypeID","请选择")
    姓名:<input  type="text" name="Name"/>
    <input  type="submit" value="查询"/>
    <a href="/test/add">添加</a>
</form>

 (3)添加的方法

 public ActionResult Add()
        {
            var list2 = dbContext.Type.ToList();
            ViewBag.TypeID = new SelectList(list2, "ID", "Name");
            return View();
        }

然后添加一个新的视图

<form  action="/test/doadd">
    <table>
        <tr><td>姓名</td><td><input type="text" name="Name" /></td></tr>
        <tr><td>日常分类</td><td>@Html.DropDownList("TypeID")</td></tr>
        <tr><td>计划开始时间</td><td><input type="date" name="BeginTime" /></td></tr>
        <tr><td>计划结束时间</td><td><input type="date" name="EndTime" /></td></tr>
        <tr><td>日程内容</td><td><input type="text" name="Content " /></td></tr>
        <tr><td>备注</td><td><input type="text" name="Remark" /></td></tr>
        <tr><td></td><td><input type="submit" value="添加" /></td></tr>
    </table>
</form>

返回的方法

 public ActionResult DoAdd(Schedule schedule)
        {
            dbContext.Schedule.Add(schedule);
            dbContext.SaveChanges();
            return Redirect("index");
                }

 然后到index视图添加添加的方法路径

总的Controllers文件代码

using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace firm.Controllers
{
    public class TestController : Controller
    {
        // GET: Test
        TestEntities1 dbContext=new TestEntities1();
        public ActionResult Index(int? TypeID,string Name)
        {
            //左链接,使用Include链接数据
            var list = dbContext.Schedule.Include("Type");
            if(TypeID!=null)
            {
                list =(DbQuery<Schedule>)  list.Where(m => m.TypeID == TypeID);
            }
            if (!string.IsNullOrEmpty(Name))
                    {
                list = (DbQuery<Schedule>)list.Where(m => m.Name.Contains("Name"));
            }
            var list2 = dbContext.Type.ToList();
            ViewBag.TypeID = new SelectList(list2, "ID", "Name");
            return View(list.ToList());
        }

        public ActionResult Delete(int id)
        {   
            //获取要删的表ID
            var sche= dbContext.Schedule.Find(id);
            dbContext.Schedule.Remove(sche);
            //保存更改
            dbContext.SaveChanges();
            return Redirect("index");
        }


        public ActionResult Add()
        {
            var list2 = dbContext.Type.ToList();
            ViewBag.TypeID = new SelectList(list2, "ID", "Name");
            return View();
        }
        public ActionResult DoAdd(Schedule schedule)
        {
            dbContext.Schedule.Add(schedule);
            dbContext.SaveChanges();
            return Redirect("index");
                }

    }
}   

index视图代码

@{
    ViewBag.Title = "Index";
}
@model List<Schedule>
<h2>Index</h2>
<form>
    @Html.DropDownList("TypeID","请选择")
    姓名:<input  type="text" name="Name"/>
    <input  type="submit" value="查询"/>
    <a href="/test/add">添加</a>
</form>
<table border="1">
    <thead>
        <tr>
            <td>日程编号</td>
            <td>日程执行人</td>
            <td>日程分类</td>
            <td>计划开始时间</td>
            <td>计划结束时间</td>
            <td>日程内容</td>
            <td>备注</td>
        </tr>

        @{
            foreach (var item in Model)
            {
                <tr>
                    <td>@item.ID</td>
                    <td>@item.Name</td>
                    <td>@item.Type.Name</td>
                    <td>@item.BeginTime</td>
                    <td>@item.EndTime</td>
                    <td>@item.Content</td>
                    <td>@item.Remark</td>
                    <td><a href="/test/Delete?id=@item.ID" onclick="return confirm('确认删除吗')">删除</a></td>
                </tr>
            }


        }
    </thead>
</table>


Add视图代码

@{
    ViewBag.Title = "Add";
}

<h2>Add</h2>
<form  action="/test/doadd">
    <table>
        <tr><td>姓名</td><td><input type="text" name="Name" /></td></tr>
        <tr><td>日常分类</td><td>@Html.DropDownList("TypeID")</td></tr>
        <tr><td>计划开始时间</td><td><input type="date" name="BeginTime" /></td></tr>
        <tr><td>计划结束时间</td><td><input type="date" name="EndTime" /></td></tr>
        <tr><td>日程内容</td><td><input type="text" name="Content " /></td></tr>
        <tr><td>备注</td><td><input type="text" name="Remark" /></td></tr>
        <tr><td></td><td><input type="submit" value="添加" /></td></tr>
    </table>
</form>