折腾了两天搞出来,但原理性的东西还不是很懂,废话不多说上图上代码

然后右键models,新建一个数据模型

注意我添加命名为lianxi

添加后如上

接下来在controllers添加控制器还有在Views中添加视图

注意控制器lianxi和视图的名字要一致,然后视图我是添加了3个分别是Index,insert,Modify,在控制器里分别有三个对应的函数

每当用URL访问视图时,他就调用了controllers对应的方法,例如

jiaEntities 就是建立模式时那个数据链接的名字

       jiaEntities db = new jiaEntities();
        public ActionResult Index()
        {
            //查询
            List<client> list = (from c in db.client select c).ToList();
            ViewData["DataList"] = list;
            return View();
        }

Views里的Index里的代码如下

Beginform相当于我们平常写HTNML里的form

@Html.ActionLink就是超链接,然后第一个参数为链接的文字表示,第2个参数为调用的控制器方法,第3个为传值,当点击某个链接,就把这个链接对于的id赋值给Cid

ViewData["DataList"] 数据就是控制器中Index传过来的,进过foreach循环得到下面结果

下面是控制器中"修改"功能的代码

        [HttpGet]
        public ActionResult Modify()
        {
            int id = Convert.ToInt32(Request.QueryString["Cid"]);
            client c1 = (from c in db.client where id == c.id select c).SingleOrDefault();
            return View(c1);
        }
        [HttpPost]
        public ActionResult Modify(client model)
        {
            client c = db.client.Where(c1 => c1.id == model.id).ToList().FirstOrDefault();
            c.name = model.name.Trim();
            db.SaveChanges();
            db.Configuration.ValidateOnSaveEnabled = true; 
            return RedirectToAction("Index", "lianxi");
        }
[httpGet]我的理解就是运行时默认调用的方法,直接从服务器获取

[httpPost]就是向服务器发送时调用的方法

所以当点击修改时就会执行第一个方法([httpGet])
点击第一项的修改之后的界面:

这个页面的代码如下

@model mvclianxi.Models.client
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Modify</title>
</head>
<body>
    @using (Html.BeginForm("Modify","lianxi",FormMethod.Post))
    {
        <table>
            <tr>
                <td>@Html.HiddenFor(model=>model.id)</td>
                <td>@Html.TextBoxFor(model => model.name)</td>
                <td><input type="submit" value="确认"/>@Html.ActionLink("<<<返回","Index","lianxi")</td>
            </tr>
        </table>
     }
</body>
</html>
@Html.HiddenFor(model=>model.id)这句去掉的话会出错,具体原因不详(ˇˍˇ) 

model就是从第一个modify还是传过来的,刚开始我也晕了...

submit的按钮就是提交这个表单,提交到哪?就是提交到下面这个修改函数了,为什么会到这?因为这个 @using (Html.BeginForm("Modify","lianxi",FormMethod.Post))

@using (Html.BeginForm("Modify","lianxi",FormMethod.Post))
@using (Html.BeginForm("Modify","lianxi",FormMethod.Post))
        public ActionResult Modify(client model)
        {
            client c = db.client.Where(c1 => c1.id == model.id).ToList().FirstOrDefault();
            c.name = model.name.Trim();
            db.SaveChanges();
            db.Configuration.ValidateOnSaveEnabled = true; 
            return RedirectToAction("Index", "lianxi");
        }

上面的函数就是用linq语句修改数据库...

接下来。。。就是添加的页面了,其实跟修改的原理都差不多,这里就不多说了直接粘代码
控制器里的:
   [HttpGet]
        public ActionResult insert()
        {
            return View();
        }

[HttpPost]
        public ActionResult insert(client model) //这个id是通过超链接带过来的
        {
            try
            {
                client c = new client();
                c.id = model.id;
                c.money = model.money;
                c.name = model.name ;
                db.client.Add(c);
                db.SaveChanges();
                return RedirectToAction("Index", "lianxi");
            }
            catch (Exception)
            {
                //指定对应跳转的视图Test下的Test.cshtml文件
                return RedirectToAction("Test", "Test");
                //return Content("添加失败" + ex.Message);
            }
        }

视图里的有两个地方
1.Index里的
    <td>@Html.ActionLink("添加", "insert")</td>

2.insert里的

@{
    ViewBag.Title = "insert";
}

@model  mvclianxi.Models.client
<h2>insert</h2>
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        @using (Html.BeginForm("Modify","lianxi",FormMethod.Post))
        {
        <table>
            <tr>
                <td>@Html.TextBoxFor(model=>model.id)</td>
                <td>@Html.TextBoxFor(model=>model.name)</td>
                <td>@Html.TextBoxFor(model=>model.money)</td>
                <td><input type="submit" value="添加"/>@Html.ActionLink("返回", "index", "lianxi")</td>
                </tr>
            </table>
        }
    </body>
    </html>

这里不小心用了他的母版页,也不影响吧,就没改了

删除:
视图中<td>@Html.ActionLink("删除", "Remove", new { Cid=a.id })</td>

控制器中
public ActionResult Remove() //这个id是通过超链接带过来的
        {
            try
            {
                //需要一个实体对象参数
                //db.Customers.Remove(new Customer() {CustomerNo = id });
                //1,创建要删除的对象
                int id = Convert.ToInt32(Request.QueryString["Cid"]);
                client c1 = (from c in db.client where c.id == id select c).SingleOrDefault();
                db.client.Remove(c1);
                db.SaveChanges();
                return RedirectToAction("Index", "lianxi");
            }
            catch (Exception)
            {
                //指定对应跳转的视图Test下的Test.cshtml文件
                return RedirectToAction("Test", "Test");
                //return Content("删除失败" + ex.Message);
            }
        }

到此结束。。。

asp.net MVC最简单的增删查改!(详)的更多相关文章

  1. nodejs连接mysql并进行简单的增删查改

    最近在入门nodejs,正好学习到了如何使用nodejs进行数据库的连接,觉得比较重要,便写一下随笔,简单地记录一下 使用在安装好node之后,我们可以使用npm命令,在项目的根目录,安装nodejs ...

  2. knockout+MVC+webapi+sqlserver完成增删查改

    快过年了,公司的事情较少,想着开始学习点新东西.这段时间一个项目用到了mvc和webapi,然后一直对knockout比较感兴趣,就想着用这个框架做一个小实例.数据库采用的是sqlserver.话不多 ...

  3. Java连接MySQL数据库及简单的增删查改操作

    主要摘自 https://www.cnblogs.com/town123/p/8336244.html https://www.runoob.com/java/java-mysql-connect.h ...

  4. mybatis实现简单的增删查改

    接触一个新技术,首先去了解它的一些基本概念,这项技术用在什么方面的.这样学习起来,方向性也会更强一些.我对于mybatis的理解是,它是一个封装了JDBC的java框架.所能实现的功能是对数据库进行增 ...

  5. EF简单的增删查改

    Add /// <summary> /// /// </summary> public void Add() { TestDBEntities2 testdb = new Te ...

  6. 一般处理程序+htm C#l简单的增删查改

    首先引用两个文件一个dll: 数据库表已创建 首先编写数据读取部分 /// <summary> /// 查询 /// </summary> /// <param name ...

  7. RavenDb学习(二)简单的增删查改

    在上一节当中已经介绍了RavenDb的文档设计模式,这一节我们要具体讲一讲如何使用api去访问RavenDb .连接RavenDb var documentStore = new DocumentSt ...

  8. Hibernate 的事物简单的增删查改

    Hibernate 是一个优秀的ORM框架体现在: 1. 面向对象设计的软件内部运行过程可以理解成就是在不断创建各种新对象.建立对象之间的关系,调用对象的方法来改变各个对象的状态和对象消亡的过程,不管 ...

  9. django与mysql实现简单的增删查改

    模型定义 from django.db import models class Grades(models.Model): g_name = models.CharField(max_length=2 ...

随机推荐

  1. WebStorm 编辑器 关闭自动保存功能及添加*星星标记

    WebStorm 关闭自动保存功能添加*星星标记为什么要关闭自动保存?      ​ 在前端项目工作当中,往往会采用自动化环境(Gulp.webpack等)当文本发生变化的时候就会自动编译代码.在we ...

  2. Paxos算法与Zookeeper分析,zab (zk)raft协议(etcd) 8. 与Galera及MySQL Group replication的比较

    mit 分布式论文集 https://github.com/feixiao/Distributed-Systems wiki上描述的几种都明白了就出师了 raft 和 zab 是类似的,都是1.先选举 ...

  3. xadmin下设置“use_bootswatch = True”无效的解决办法

    环境: python 2.7 django 1.9 xadmin采用源代码的方式引入到项目中 问题: 在xadmin使用的过程中,设置“use_bootswatch = True”,企图调出主题菜单, ...

  4. Python赋值运算及流程控制

    1. 内置函数 1> len:统计元素长度 str1 = 'wonderful' print(len(str1)) result: li = [,,] print(len(li)) result ...

  5. Linux基础学习-Docker学习笔记

    Docker安装 1 官方网站访问速度很慢,帮助文档 2 国内中文网站,帮助文档 [root@qdlinux ~]# yum remove docker \ docker-client \ docke ...

  6. spring,spring mvc,mybatis 常用注解

    文章来源:https://www.cnblogs.com/hello-tl/p/9209063.html  0.在spring,soring mvc, mybistis 中的常用注解有一下 <! ...

  7. Aizu-ALDS1_3_A:Stack

    D - Stack Write a program which reads an expression in the Reverse Polish notation and prints the co ...

  8. 【UOJ#51】【UR #4】元旦三侠的游戏(博弈论)

    [UOJ#51][UR #4]元旦三侠的游戏(博弈论) 题面 UOJ 题解 考虑暴力,\(sg[a][b]\)记录\(sg\)函数值,显然可以从\(sg[a+1][b]\)和\(sg[a][b+1]\ ...

  9. Python第三方库之openpyxl(8)

    Python第三方库之openpyxl(8) 饼图 饼图将数据绘制成一个圆片,每个片代表整体的百分比.切片是按顺时针方向绘制的,0在圆的顶部.饼图只能取一组数据.该图表的标题将默认为该系列的标题. 2 ...

  10. BNUOJ 1055 走迷宫2

    走迷宫2 Time Limit: 1000ms Memory Limit: 65535KB   64-bit integer IO format: %lld      Java class name: ...