创建数据库表如下:

生成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. 马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动(转)

    马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动 马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作 马士兵hadoop第三课:java开发hdfs 马士兵hadoop第 ...

  2. UVA 10003 Cutting Sticks(区间dp)

    Description    Cutting Sticks  You have to cut a wood stick into pieces. The most affordable company ...

  3. POJ3539 Elevator

    Time Limit: 4000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description Edward wor ...

  4. 【webstrom】webstrom打开多个项目,webstrom常用快捷键

    1.webstrom打开多个项目 默认情况下一次只能打开一个项目,如果需要打开多个就按照下面的方法 File -> settings -> Directories -> Add Co ...

  5. 转 Centos下安装apahce的configure: error: APR not found. Please read the documentation解决办法

    转自: http://www.cnblogs.com/Anker/p/3355573.html 今天从Apache官网上http://httpd.apache.org/下载httpd web服务器,由 ...

  6. PHP错误捕获处理

    PHP错误捕获处理 一般捕获错误使用的方法是: try{ ...}catch(Exception $e){ echo $e->getMessage();} 或者 set_exception_ha ...

  7. yii批量插入数据

    现在有如下数据: 查看代码   打印 1 $user=array( 2     0=>array('id'=>1,'name'=>'张三'), 3     0=>array(' ...

  8. 使用Python计算研究生学分绩(绩点)

    最近看了CSDN上一个专栏<Python爬虫入门教程>,其中最后一篇作者写了个例子,用爬虫计算山东大学绩点,顿时想到前一阵子搞测评的时候还得拿计算器一点点算自己的平均学分绩,也想写一个自己 ...

  9. 洛谷—— P1440 求m区间内的最小值

    https://www.luogu.org/problemnew/show/P1440 题目描述 一个含有n项的数列(n<=2000000),求出每一项前的m个数到它这个区间内的最小值.若前面的 ...

  10. OS | Socket

    TCP 创建socket: int socket(int domain, int type, int protocol); AF = Address FamilyPF = Protocol Famil ...