创建数据库表如下:

生成EF模型

//------------------------------------------------------------------------------
// <auto-generated>
// 此代码是根据模板生成的。
//
// 手动更改此文件可能会导致应用程序中发生异常行为。
// 如果重新生成代码,则将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------ namespace MvcApplicationStudy.Models
{
using System;
using System.Collections.Generic; public partial class UserInfo
{
public int ID { get; set; }
public string UserName { get; set; }
public string UserPwd { get; set; }
public System.DateTime RegTime { get; set; }
}
}

  创建控制器HomeController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplicationStudy.Models; namespace MvcApplicationStudy.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/ public ActionResult Index()//注册页面
{
if (!string.IsNullOrEmpty(Request["msg"]))
{
ViewBag.Msg = Request["msg"];
}
return View();
}
public ActionResult Register()//从表单中获取参数
{
UserInfo userInfo = new UserInfo();
userInfo.UserName = Request["txtName"].ToString();
userInfo.UserPwd = Request["txtPwd"].ToString();
userInfo.RegTime = DateTime.Now;
TestEntities dbEntity = new TestEntities();
dbEntity.UserInfo.Add(userInfo);
if (dbEntity.SaveChanges() > 0)
{
return RedirectToAction("Index", "Home", new { msg = "注册成功" });
//return Content("注册成功");
}
else
{
return RedirectToAction("Index", "Home", new { msg="注册失败"});
}
} //表单中表单元素的name属性取值与当前方法中参数的名称一致,会自动填充。
public ActionResult Register2(string txtName, string txtPwd)
{
UserInfo userInfo = new UserInfo();
userInfo.UserName = txtName;
userInfo.UserPwd = txtPwd;
userInfo.RegTime = DateTime.Now;
TestEntities dbEntity = new TestEntities();
dbEntity.UserInfo.Add(userInfo);
if (dbEntity.SaveChanges() > 0)
{
return Content("注册成功");
}
else
{
return Content("注册失败");
}
} //自动填充(如果表单中表单元素的name属性的取值与实体类中属性的名字保持一致,会自动填充。)
/*public ActionResult Register3 (UserInfo userInfo)
{ userInfo.RegTime = DateTime.Now;
TestEntities dbEntity = new TestEntities();
dbEntity.UserInfo.Add(userInfo);
if (dbEntity.SaveChanges() > 0)
{
return Content("注册成功");
}
else
{
return Content("注册失败");
} }*/
}
}

  添加Index视图

@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<form method="post" action="/Home/Register2">
用户名<input type="text" name ="txtName" id="txtName" />
<br />
密码 <input type="password" name="txtPwd" id="txtPwd" />
<br />
<input type="submit" value="注册" />@ViewBag.Msg
</form>
</div>
</body>
</html>

  生成页面如下

MVC入门——增的更多相关文章

  1. 25、ASP.NET MVC入门到精通——Spring.net-业务层仓储

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 上一节,我们已经把项目框架的雏形搭建好了,那么现在我来开始业务实现,在业务实现的过程当中,不断的来完善我们现有的框架. 1.假设我们来做一个 ...

  2. 26、ASP.NET MVC入门到精通——后台管理区域及分离、Js压缩、css、jquery扩展

    本系列目录:ASP.NET MVC4入门到精通系列目录汇总 有好一段时间没更新博文了,最近在忙两件事:1.看书,学习中...2.为公司年会节目做准备,由于许久没有练习双截棍了,难免生疏,所以现在临时抱 ...

  3. asp.net mvc 入门资料

    七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC http://www.cnblogs.com/powertoolsteam/p/MVC_one.html 无废话MVC入门教程 ...

  4. ASP.NET MVC 入门8、ModelState与数据验证

    原帖地址:http://www.cnblogs.com/QLeelulu/archive/2008/10/08/1305962.html ViewData有一个ModelState的属性,这是一个类型 ...

  5. ASP.NET MVC 入门系列教程

    ASP.NET MVC 入门系列教程 博客园ASP.NET MVC 技术专题 http://kb.cnblogs.com/zt/mvc/ 一个居于ASP.NET MVC Beta的系列入门文章,有朋友 ...

  6. Spring MVC 入门教程示例 (一)

    今天和大家分享下  Spring MVC  入门教程 首先还是从 HelloWorld  web 工程开始 -------------------------- 1.首先创建一个Maven Web工程 ...

  7. Asp.net MVC入门视频教程

    编程开发 > Asp.net视频教程 > Asp.net MVC入门视频教程 > 1.传统web处理方式和mvc处理方式 上传日期:2014-08-16 10:02:45  相关摘要 ...

  8. [转]ASP.NET MVC 入门8、ModelState与数据验证

    ViewData有一个ModelState的属性,这是一个类型为ModelStateDictionary的ModelState类型的字典集合.在进行数据验证的时候这个属性是比较有用的.在使用Html. ...

  9. 分享一个自己写的MVC+EF “增删改查” 无刷新分页程序

    分享一个自己写的MVC+EF “增删改查” 无刷新分页程序 一.项目之前得添加几个组件artDialog.MVCPager.kindeditor-4.0.先上几个效果图.      1.首先建立一个数 ...

随机推荐

  1. DispatcherServlet与ContextLoaderListener的对比

    1. 从DispatcherServlet和ContextLoaderListener的初始化过程可以看出,二者分别会生成一个WebApplicationContext,且以不同的attrName注册 ...

  2. JavaScript 在双引号之间引用变量

    可以采用   ' +  变量 +  ' .

  3. ORA-12514: TNS:listener does not currently know of service requested in connect

    https://blog.csdn.net/mchdba/article/details/50166153

  4. 牛客网 Wannafly挑战赛5 B.可编程拖拉机比赛-ceil()函数+floor()函数

    可编程拖拉机比赛 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 65536K,其他语言131072K64bit IO Format: %lld 题目描述 “这个比赛,归根结底就是控制一 ...

  5. CentOS6.7安装部署LNMP(nginx1.8.0+php5.6.10+mysql5.6.12)

    IP-10.0.0.8 1.安装nginx mkdir -p /server/tools cd /server/tools yum install -y pcre pcre-devel openssl ...

  6. spring beans 接口

  7. IntelliJ IDEA重构技巧收集

    https://segmentfault.com/a/1190000002488608(重命名文件) http://www.jianshu.com/p/ab298b46bf50(快速生成方法) htt ...

  8. SpringUtils写法

    @Componentpublic class SpringUtils implements ApplicationContextAware { @Override public void setApp ...

  9. Android View源码解读:浅谈DecorView与ViewRootImpl

    前言 对于Android开发者来说,View无疑是开发中经常接触的,包括它的事件分发机制.测量.布局.绘制流程等,如果要自定义一个View,那么应该对以上流程有所了解.研究.本系列文章将会为大家带来V ...

  10. SSH错误:packet_write_wait: Connection to 10.57.19.250 port 22: Broken pipe

    现象:ssh连接以后,服务器会主动断开连接,wireshark抓包,发线服务器会tcp rst,断开ssh连接 解决尝试:1.修改会话超时时间:2.客户端主动间隔性向服务器发送保活报文:3.服务端主动 ...