学习MVC3心得

学习了一段时间MVC3,做了账户登录功能和简单的增删改查功能,略有心得,拿来分享。

首先是账户这一块:有登录,登出,注册的功能。

登录的代码如下:

View Code
 1 [HttpPost]
2 public ActionResult LogOn(AccountInformation model, string returnUrl) {
3 if(ModelState.IsValid) {
4 if(Account.Initial.ValidateUser(model)) {
5 FormsService.SignIn(model.Account_UserName,true);
6 return RedirectToAction("Index", "User");
7
8 } else {
9 ModelState.AddModelError("", "用户名或密码不正确,请检查后重新输入!");
10
11 }
12 }
13 // If we got this far, something failed, redisplay form
14 return View(model);
15 }

登出的代码比较简单:

View Code
1 public ActionResult LogOff() {
2 FormsService.SignOut();
3 return RedirectToAction("Index", "Account");
4 }

注册的代码control部分如下:

View Code
 1 public ActionResult Register() {
2 return View();
3 }
4
5 [HttpPost]
6 public ActionResult Register(AccountInformation model) {
7 if(ModelState.IsValid) {
8 if(Account.Initial.RegisterUser(model)) {
9 return RedirectToAction("Index");
10 }
11 }
12 return View(model);
13 }

model部分如下:

View Code
 1 public bool RegisterUser(AccountInformation model) {
2 using (AccountEntities1 db = new AccountEntities1())
3 {
4 if(model != null) {
5 AccountInformation aa = new AccountInformation();
6 aa.Account_Password = model.Account_Password;
7 aa.Account_UserName = model.Account_UserName;
8 aa.Account_Email = model.Account_Email;
9 db.AddToAccountInformation(aa);
10 db.SaveChanges();
11 return true;
12 }
13 }
14 return false;
15 }

view部分如下:

View Code
 1 @model LoginMVC.Models.AccountInformation
2
3 @{
4 ViewBag.Title = "Register";
5 Layout = "~/Views/Shared/_Layout.cshtml";
6 }
7
8 <h2>Create a New Account</h2>
9 <p>
10 Use the form below to create a new account.
11 </p>
12 <p>
13 Passwords are required to be a minimum of @ViewBag.PasswordLength characters in length.
14 </p>
15
16 <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
17 <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
18
19 @using (Html.BeginForm()) {
20 @Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")
21 <div>
22 <fieldset>
23 <legend>Account Information</legend>
24
25 <div class="editor-label">
26 @Html.LabelFor(m =>m.Account_UserName)
27 </div>
28 <div class="editor-field">
29 @Html.TextBoxFor(m => m.Account_UserName)
30 @Html.ValidationMessageFor(m => m.Account_UserName)
31 </div>
32
33 <div class="editor-label">
34 @Html.LabelFor(m => m.Account_Email)
35 </div>
36 <div class="editor-field">
37 @Html.TextBoxFor(m => m.Account_Email)
38 @Html.ValidationMessageFor(m => m.Account_Email)
39 </div>
40
41 <div class="editor-label">
42 @Html.LabelFor(m => m.Account_Password)
43 </div>
44 <div class="editor-field">
45 @Html.PasswordFor(m => m.Account_Password)
46 @Html.ValidationMessageFor(m => m.Account_Password)
47 </div>
48 <p>
49 <input type="submit" value="Register" />
50 </p>
51 </fieldset>
52
53 </div>
54 }

这样就完成了用户账户的注册登录这一部分了。

posted @ 2011-08-03 10:22  zhuhaorain  阅读(557)  评论(0编辑  收藏  举报