Create a database table as follows:
Generate EF Models
------------------------------------------------------------------------------//<auto-generated>// This code is generated based on the template. manually changing this file may cause unexpected behavior in the application. If the code is regenerated, manual changes to this file will be overwritten. </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;}} }
Create a controller 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 ()//Register page {if (!string. IsNullOrEmpty (request["MSG"])) {viewbag.msg = request["MSG"]; } return View (); The public ActionResult Register ()//Gets the parameter {UserInfo UserInfo = new UserInfo () from the form; 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 = "Registration succeeded" }); Return COntent ("registered success"); } else {return redirecttoaction ("Index", "Home", new {msg= "registration failed"}); }}//The Name property of the form element in the form is automatically populated with the same value as the parameter in the current method. 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 ("registered successfully"); } else {return Content ("registration failed"); }}//AutoFill (if the Name property of the form element in the form matches the name of the attribute in the entity class, it is automatically populated.) )/*public ActionResult Register3 (UserInfo UserInfo) {userinfo.regtime = DateTime.Now; Testentities dbentity = new Testentities (); DBENTITY.USERINFO.ADD (USerinfo); if (dbentity.savechanges () > 0) {return Content ("registered successfully"); } else {return Content ("registration failed"); } }*/ }}
Add an index view
@{ Layout = null;} <! DOCTYPE html>
The build page is as follows
Getting Started with MVC--increasing