方便自己复习使用,不多说,上代码。

方便自己复习使用,不多说,上代码。

OneDrive链接:毕竟垃圾就不放了(OneDrive - Personal/VS-Code-Demo/)

Home控制器

using EFDemo5.Models;
using System.Linq;
using System.Web.Mvc;

namespace EFDemo5.Controllers
{
    public class HomeController : Controller
    {
        NewsEntities1 ns = new NewsEntities1();
        public ActionResult Index()
        {
            //1、返回所有
            var nes = ns.News_Type.ToList();
            ViewBag.msg = nes;
            return View();
        }
        /// <summary>
        /// 增加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddNews()
        {
             var newstype = ns.News_Type.ToList();
              return View(newstype);
        }

        /// <summary>
        /// 增加新闻功能
        /// </summary>
        /// <returns></returns>
        /// 
        public ActionResult Add(News_Type News_Type)
        {
            //1、添加对象
            ns.News_Type.Add(News_Type);
            //2、保存返回大于1成功!
            int rs = ns.SaveChanges();
            return RedirectToAction("Index", "Home");
        }
        /// <summary>
        /// 修改页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            //1、查询原有id传递到修改页面
            var usnews = ns.News_Type.FirstOrDefault(x => x.Type_id == id);
            var usnewsDescrip = ns.News_Type.ToList();
            return View(usnews);
        }
        /// <summary>
        /// 修改功能
        /// </summary>
        /// <returns></returns>
        public ActionResult EditNews(News_Type news_Type)
        {
            //1、原有对象
            var Enews = ns.News_Type.FirstOrDefault(x => x.Type_id == news_Type.Type_id);
            //2、重新赋值
            Enews.Type_Name = news_Type.Type_Name;
            Enews.Type_Description = news_Type.Type_Description;
            //3、提交保存
            ns.SaveChanges();
            return RedirectToAction("Index", "Home");
        }
        /// <summary>
        /// 删除功能
        /// </summary>
        /// <returns></returns>
        public ActionResult DeleteNews(int id)
        {
            //1、查询原有id对象
            var news = ns.News_Type.FirstOrDefault(x => x.Type_id == id);
            //2、移除对象
            ns.News_Type.Remove(news);
            //3、保存
            ns.SaveChanges();
            //4、返回Index 
            return RedirectToAction("Index","Home");
        }
    }
}

Index视图

@{
    Layout = null;
}
@using EFDemo5.Models;
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width,initial-scake=1.0" />
    <title>Index</title>
    <link href="~/Content/css/bootstrap.min.css" rel="stylesheet" />
    <script src="~/Content/js/bootstrap.min.js"></script>
    <script src="~/Content/js/._jquery.min.js"></script>
    <style>
        .table {
            max-width: 60%;
            text-align: center;
            margin: 0 auto;
        }
        th {
            text-align: center;
        }
    </style>
    <script type="text/javascript">
     
    </script>
</head>
<body>
    <div class="box">
        <button type="button" class="btn btn-default" id="Increase"><a href="/Home/AddNews">增加</a></button>
        <table class="table table-bordered">
            <tr><th>编号</th><th>名称</th><th>类别</th><th>操作</th></tr>
            @foreach (var item in (List<News_Type>)ViewBag.msg)
            {
                <tr>
                    <td>@item.Type_id</td>
                    <td>@item.Type_Name</td>
                    <td>@item.Type_Description</td>
                    <td><button type="button" class="btn btn-default"><a href="/Home/Edit?ID=@item.Type_id">修改</a></button><button type="button" class="btn btn-default"><a href="/Home/DeleteNews?ID=@item.Type_id">删除</a></button></td>
                </tr>
            }

        </table>
    </div>
</body>
</html>

AddNews视图

@{
    Layout = null;
}
@using EFDemo5.Models;
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>添加新闻</title>
    <link href="~/Content/css/bootstrap.min.css" rel="stylesheet" />
    <script src="~/Content/js/bootstrap.min.js"></script>
    <script src="~/Content/js/._jquery.min.js"></script>
    <style>
        #box {
            /*display:flex;*/
            text-align: center;
            margin: 0 auto;
            margin-top: 90px;
            max-width: 60%;
        }
    </style>
    <script>
        //var btn = document.getElementById('intp').value();
        //btn.onclick = function () {
        //    if (btn > 0) {
        //        alert("成功!");
        //    }
        //    else {
        //        alert("失败!");
        //    }
        //}
    </script>
</head>
<body>
    <div id="box">
        <form class="form-horizontal" role="form" action="Add">
            <div class="form-group">
                <label class="col-sm-2 control-label">名称</label>
                <div class="col-sm-10">
                    <input class="form-control" id="focusedInput" type="text" name="Type_Name" value="" placeholder="请输入新闻名称" >
                </div>
            </div>
            <div class="form-group">
                <label for="" class="col-sm-2 control-label">新闻类别</label>
                <div class="col-sm-10">
                    <select id="box2" class="form-control" name="Type_Description">
                        @foreach (var item in (List<News_Type>)Model)
                        {
                            <option value="@item.Type_Description">@item.Type_Description</option>
                        }
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" id="bt" class="btn btn-default" value="">添加</button>
                </div>
            </div>
        </form>
    </div>
</body>
</html>

Edit视图

@{
    Layout = null;
}
@using EFDemo5.Models;
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>修改新闻</title>
    <link href="~/Content/css/bootstrap.min.css" rel="stylesheet" />
    <script src="~/Content/js/bootstrap.min.js"></script>
    <script src="~/Content/js/._jquery.min.js"></script>
    <style>
        #box {
            /*display:flex;*/
            text-align: center;
            margin: 0 auto;
            margin-top: 90px;
            max-width: 60%;
        }
    </style>
    <script>
        //var btn = document.getElementById('intp').value();
        //btn.onclick = function () {
        //    if (btn > 0) {
        //        alert("成功!");
        //    }
        //    else {
        //        alert("失败!");
        //    }
        //}
    </script>
</head>
<body>
    <div id="box">
        <form class="form-horizontal" role="form" action="EditNews">
            <div class="form-group">
                <label class="col-sm-2 control-label">名称</label>
                <div class="col-sm-10">
                    <input type="hidden" name="Type_id" id="" value="@Model.Type_id"/>
                    <input class="form-control" id="focusedInput" type="text" name="Type_Name" value="@Model.Type_Name" placeholder="请修改新闻名称">
                </div>
            </div>
            <div class="form-group">
                <label for="" class="col-sm-2 control-label">新闻类别</label>
                <div class="col-sm-10">
                    <select id="box2" class="form-control" name="Type_Description">
                            <option value="@Model.Type_Description">@Model.Type_Description</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" id="" class="btn btn-default" value="">修改</button>
                </div>
            </div>
        </form>
    </div>
</body>
</html>

效果图

此处有个bug没有处理 (类别设置修改的时候没有使用主外键)