MVC 是我大学学的比较弱的,甚至不懂原理。(那时候都在准备蓝桥杯 软件杯比赛。)

在重新学 肯定要学MVC

现在知道了为什么叫MVC了  MVC是怎么工作的

MVC 是一个设计模式

控制器(Controller)和视图(View)通过model传输 逻辑和显示 互补干扰

第一个例子--增删查改

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models; namespace WebApplication1.Controllers
{
public class DefaultController : Controller
{
// GET: Default
public ActionResult Index()
{
string sql = "select * from [User]";
DataTable dataTable = SqlseverDBhelper.Query(sql).Tables[0];
return View(dataTable);
} [HttpGet]
public ActionResult Add()
{
return View(); }
[HttpPost]
public ActionResult Add(User us)
{
string sql = string.Format("insert into [User](Uname,Uphone,Uadd,[Money]) values('{0}','{1}','{2}','{3}')", us.Uname, us.Uphone, us.Uadd, us.Money);
if (SqlseverDBhelper.ExecuteNonQuery(sql)>0)
{
return Redirect("/Default/Index");
}
else
{
return View();
}
//重定向 }
[HttpGet]
public ActionResult Del(int id)
{
string sql = "delete [User] where id=" + id;
if (SqlseverDBhelper.ExecuteNonQuery(sql)>0)
{
return Redirect("/Default/Index"); }
else
{
return Redirect("/Default/Index"); }
} [HttpGet]
public ActionResult Edit(int id)
{ string sql = "select * from [User] where id="+id;
DataTable dataTable = SqlseverDBhelper.Query(sql).Tables[0];
User us = new User();
us.id =Convert.ToInt32(dataTable.Rows[0]["id"]);
us.Uname = dataTable.Rows[0]["Uname"].ToString();
us.Uphone = dataTable.Rows[0]["Uphone"].ToString();
us.Uadd = dataTable.Rows[0]["Uadd"].ToString();
us.Money =Convert.ToDecimal(dataTable.Rows[0]["Money"]);
return View(us); }
[HttpPost]
public ActionResult Edit(User us)
{
string sql = string.Format("update [User] set Uname='{0}',Uphone='{1}',Uadd='{2}',[Money]='{3}' where id='{4}'", us.Uname, us.Uphone, us.Uadd, us.Money,us.id);
if (SqlseverDBhelper.ExecuteNonQuery(sql) > 0)
{
return Redirect("/Default/Index");
}
else
{
return View();
}
}
}
}

  

发现自己以前学的还没有忘 一些属性方法 直接可以出来  SQL语句也可以写出来

注意一点

input 的不可编辑disabled="disabled"这样定义之后被禁用的 input 元素既不可用,也不可点击 ,不可提交 不能传到后台。第二:readonly="readonly" 只读字段是不能修改的

推荐使用第2个 然后去设置css样式

MVC——再探MVC——增删查改的更多相关文章

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

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

  2. asp.net MVC最简单的增删查改!(详)

    折腾了两天搞出来,但原理性的东西还不是很懂,废话不多说上图上代码 然后右键models,新建一个数据模型 注意我添加命名为lianxi 添加后如上 接下来在controllers添加控制器还有在Vie ...

  3. 4.在MVC中使用仓储模式进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-using-the-repository-pattern-in-mvc/ 系列目录: ...

  4. backbonejs mvc框架的增删查改实例

    一:开发环境 coffeescript和nodejs需要先安装,没装网上自己查安装步骤. 代码编写环境及esp框架下载: esp框架下载地址:https://github.com/nonocast/e ...

  5. VS 自动创建带增删查改的MVC网站

    VS 自动创建带增删查改的MVC网站 MVC.Net教程   废话放在前头,说一下这个文章的缘起某天某妹纸找我,说这个MVC的创建不太会,要记一下controllers.models.还有页面引用的东 ...

  6. 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  7. 5.在MVC中使用泛型仓储模式和工作单元来进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  8. 在MVC中使用泛型仓储模式和工作单元来进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  9. 在MVC中使用泛型仓储模式和依赖注入实现增删查改

    标签: 原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository ...

随机推荐

  1. Acwing200 Hankson的趣味题

    原题面:https://www.acwing.com/problem/content/202/ 题目大意:gcd(x,a0)=a1,lcm(x,b0)=b1,问你有多少满足条件的正整数x. 输入描述: ...

  2. netty权威指南学习笔记四——TCP粘包/拆包之粘包问题解决

    发生了粘包,我们需要将其清晰的进行拆包处理,这里采用LineBasedFrameDecoder来解决 LineBasedFrameDecoder的工作原理是它依次遍历ByteBuf中的可读字节,判断看 ...

  3. s5pc100开发板Nand flash移植

    相关软件下载地址:http://pan.baidu.com/s/16yo8Y fsc100开发板 交叉编译工具:arm-cortex_a8-linux-gnueabi-gcc Ÿ   添加针对我们平台 ...

  4. expect 自动输入密码

    测试环境centos 6.5 7.4 1.远程登陆执行命令  scp #!/bin/bash expect -c ' set timeout 10000 spawn ssh root@192.168. ...

  5. cf 785#

    23333再次水惨了..(心酸啊) A题呵呵呵呵呵 #include<cstdio> #include<iostream> using namespace std; int m ...

  6. 九、SAP中使用定义时间及使用sy-uzeit取当前时间

    一.sy-uzeit为取当前时间函数,类型t为时间类型,代码如下: 二.输出结果如下:

  7. [Updating]点分治学习笔记

    Upd \(2020/2/15\),又补了一题 LuoguP2664 树上游戏 \(2020/2/14\),补了一道例题 LuoguP3085 [USACO13OPEN]阴和阳Yin and Yang ...

  8. python_@propetry

    @propetry的作用就是让一个方法可以当成属性被调用. @property的实现比较复杂,我们先考察如何使用.把一个getter方法变成属性,只需要加上@property就可以了,此时,@prop ...

  9. s5pc100开发板linux内核移植

    相关软件下载地址:http://pan.baidu.com/s/16yo8Y 应用于FSC100开发板 交叉编译工具:arm-cortex_a8-linux-gnueabi-gcc linux-2.6 ...

  10. GitHub 网站上不去/加载慢/加载不全 解决办法

    1. 当你打开你的 GitHub 2. F12 进入检查页面,点击 network 3. 找到变红的字段右键复制连接 4. 打开 DNS 查询网站,输入你复制的网址,点击查询 5. 选择国内的 ip ...