• LocalDB数据库的增删改查方法


    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    using Movies.Models;
    
    namespace Movies.Controllers
    {
    
    
    
        //LocalDB的增删改查方法
        public class MoviesController : Controller
        {
            private MovieDBContext db = new MovieDBContext();
    
            // GET: /Movies/主页面
            public ActionResult Index()
            {
                //查询全部
                return View(db.Movies.ToList());
            }
    
            // GET: /Movies/Details/5 电影详细
            public ActionResult Details(int? id)
            {
                ////参数不正确,直接抛出对应的HttpStatusCodeResult结果
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                //根据id进行查询,查询出来的结果放在Movie类的Movie对象里
                Movie movie = db.Movies.Find(id);
                //如果一个电影没有找到,返回HttpNotFound()
                if (movie == null)
                {
                    return HttpNotFound();
                }
                return View(movie);
            }
    
            // GET: /Movies/Create添加一条新电影,返回一个视图
            public ActionResult Create()
            {
                return View();
            }
    
            // POST: /Movies/Create
          
            [HttpPost]
            [ValidateAntiForgeryToken]
            //在添加电影的视图里获取值并添加到数据库,添加成功返回主页面
            public ActionResult Create([Bind(Include="ID,Title,ReleaseDate,Genre,Price")] Movie movie)
            {
                if (ModelState.IsValid)
                {
                    db.Movies.Add(movie);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
    
                return View(movie);
            }
    
            // GET: /Movies/Edit/5 修改电影详细,返回一个修改界面的视图,并把查出数据movie用强类型传达到界面
            public ActionResult Edit(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Movie movie = db.Movies.Find(id);
                if (movie == null)
                {
                    return HttpNotFound();
                }
                return View(movie);
            }
    
            // POST: /Movies/Edit/5
            // 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关 
            // 详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598
            [HttpPost]
            [ValidateAntiForgeryToken]
            //在修改视图里进行修改,这是修改方法,修改后返回主界面。强类型传数据
            public ActionResult Edit([Bind(Include="ID,Title,ReleaseDate,Genre,Price")] Movie movie)
            {
                if (ModelState.IsValid)
                {
                    db.Entry(movie).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(movie);
            }
    
    
            //控制器返回一个删除的视图,并把查出数据movie用强类型传达到界面
            // GET: /Movies/Delete/5 删除电影
            public ActionResult Delete(int? id)
            {
                if (id == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Movie movie = db.Movies.Find(id);
                if (movie == null)
                {
                    return HttpNotFound();
                }
                return View(movie);
            }
    
            // POST: /Movies/Delete/5
            [HttpPost, ActionName("Delete")]
            [ValidateAntiForgeryToken]
            //删除的方法,删除后直接返回主界面
            public ActionResult DeleteConfirmed(int id)
            {
                Movie movie = db.Movies.Find(id);
                db.Movies.Remove(movie);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
    
            protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    db.Dispose();
                }
                base.Dispose(disposing);
            }
        }
    }
    @*强类型声明,后面是引用的命名空间*@
    @model Movies.Models.Movie
    @{
        ViewBag.Title = "Details";
    }
    
    <h2>Details</h2>
    
    <div>
        <h4>Movie</h4>
        <hr />
        @*<dl>
            标签定义了定义列表(definition list)。
            <dl>
                标签用于结合
                <dt> (定义列表中的项目)和
                <dd>
                    (描述列表中的项目)。*@
                    <dl class="dl-horizontal">
                        <dt>
                            @*主页里的列名*@
                            @Html.DisplayNameFor(model => model.Title)
                        </dt>
    
                        <dd>
                            @Html.DisplayFor(model => model.Title)
                        </dd>
    
                        <dt>
                            @Html.DisplayNameFor(model => model.ReleaseDate)
                        </dt>
    
                        <dd>
                            @Html.DisplayFor(model => model.ReleaseDate)
                        </dd>
    
                        <dt>
                            @Html.DisplayNameFor(model => model.Genre)
                        </dt>
    
                        <dd>
                            @Html.DisplayFor(model => model.Genre)
                        </dd>
    
                        <dt>
                            @Html.DisplayNameFor(model => model.Price)
                        </dt>
    
                        <dd>
                            @Html.DisplayFor(model => model.Price)
                        </dd>
    
                    </dl>
    </div>
    <p>
        @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
        @Html.ActionLink("Back to List", "Index")
    </p>
  • 相关阅读:
    Linux常用命令大全
    深入浅出Shell编程: Shell 变量
    hadoop权威指南实例源代码网址
    hadoop基本操作命令
    Hadoop集群搭建
    JobStracker与TaskStracker的区别联系
    Linux解压命令
    Java程序作为windows服务运行
    【leetcode】1274. Number of Ships in a Rectangle
    【leetcode】1272. Remove Interval
  • 原文地址:https://www.cnblogs.com/275147378abc/p/4759802.html
Copyright © 2020-2023  润新知