您的位置:首页 > 数据库

MVC对数据库增删改查,使用实体模型

2017-08-03 18:31 323 查看
使用了MVC之后,对数据库的操作变得简单多了。创建MVC后,在model层添加一个ADO.NET实体数据模型。对应于我们要操作的数据库的表。

在控制器编写如下代码,注意一个控制器应对应于一个视图,如果一个视图对应于两个控制器,那么在行为前加上[HttpPost],那么此行为为post时执行。默认为GET。

DbContext为上下文对象,很重要的一个要记住的类。我们新建的实体数据模型名为 Database1Entities;

public ActionResult Index()

        {

            DbContext db = new Database1Entities();//子类赋值给父类

            IQueryable<Table> t1 = db.Set<Table>();//将所有数据传入

            return View(t1);//返回到VIEW中

        }
//控制器所有代码如下

using System;

using System.Collections.Generic;

using System.Data.Entity;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using crud.Models;

using System.Data.Entity.Migrations;

using System.Web.Routing;

namespace crud.Controllers

{

    public class CRUDController : Controller

    {

        //

        // GET: /CRUD/

        public ActionResult Index()

        {

            DbContext db = new Database1Entities();

            IQueryable<Table> t1 = db.Set<Table>();

            return View(t1);

        }

        public ActionResult create()

        {

            return View();

        }

        [HttpPost]

        public ActionResult create(Table t1)

        {

            DbContext db = new Database1Entities();

            db.Set<Table>().Add(t1);

            int res= db.SaveChanges();

            if (res>0)

            {

                return Redirect(@Url.Action("index","crud"));

            }

            else

            {

                return Redirect(@Url.Action("create","crud"));

            }

        }

        public ActionResult delete(int Id)

        {

            DbContext db = new Database1Entities();

            var deleteVal = db.Set<Table>().Where(u => u.Id == Id).FirstOrDefault();

            db.Set<Table>().Remove(deleteVal);

            int res=db.SaveChanges();

            if (res>0 )

            {

               return Redirect(@Url.Action("INDEX","CRUD"));

            }

            else

            {

                return Redirect(@Url.Action("", ""));

            }

        }

        public ActionResult change(int Id)

        {

            DbContext db = new Database1Entities();

            ViewData.Model=db.Set<Table>().Where(u=>u.Id==Id).FirstOrDefault();

            //IQueryable<Table> iq = db.Set<Table>().Where(u => u.Id == Id);

            return View();

        }

        [HttpPost]

        public ActionResult change(Table table)

        {

            DbContext db = new Database1Entities();

            IQueryable<Table> iq = db.Set<Table>().Where(u => u.Id == table.Id);

            db.Set<Table>().AddOrUpdate(table);

            db.SaveChanges();

            return Redirect(@Url.Action("index","CRUD"));

        }

    }

}

//VIEW代码如下

@using crud.Models

@model IQueryable< crud.Models.Table>

@{

    Layout = null;

}

<!DOCTYPE html>

<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <title>Index</title>

</head>

<body>

    <div>

        @Html.ActionLink("Add", "create", "CRUD");

        <br />

        <table border="1">

            <tr>

                <th>id</th>

                <th>username</th>

                <th>delete</th>

            </tr>

            @foreach (Table table in Model)

            {

                <tr>

                    <td>@table.Id</td>

                    <td>@table.username</td>

                    <td>@Html.ActionLink("delete", "delete", "CRUD", new RouteValueDictionary(new {Id=@table.Id}),null)</td>

                    <td>@Html.ActionLink("change","change","CRUD",new RouteValueDictionary(new {Id= @table.Id}),null)</td>

                </tr>

            }

        </table>

    </div>

</body>

</html>

//创建代码如下

@model crud.Models.Table

@{

    Layout = null;

}

<!DOCTYPE html>

<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <title>create</title>

</head>

<body>

    <div>

a370
        @using (Html.BeginForm("create", "crud", FormMethod.Post))

        {

            <span>username</span>

            @Html.TextBoxFor(u => u.username)

            <br />

            <input type="submit" value="Click" />

        }

    </div>

</body>

</html>

//修改代码如下

@model crud.Models.Table

@{

    Layout = null;

}

<!DOCTYPE html>

<html>

<head>

    <meta name="viewport" content="width=device-width" />

    <title>change</title>

</head>

<body>

    <div>

        @using (Html.BeginForm("change", "crud", FormMethod.Post))

        {

            @Model.Id

            @Html.HiddenFor(n => n.Id)

            @Html.TextBoxFor(n=>n.username)

            <input type="submit" value="change" />

          }

    </div>

</body>

</html>

完成
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: