MVC4制作网站教程第二章 用户修改资料2.4

2025-05-29 0 73

一、用户
1.1用户注册
1.2用户登录
1.3修改密码
1.4修改资料

在用户登陆成功后要跳转到一个页面,暂且叫做用户中心吧。在【UserController】添加[default] action

?

1

2

3

4

5

6

7
[UserAuthorize]

public ActionResult Default()

{

userRsy = new UserRepository();

var _user = userRsy.Find(UserName);

return View(_user);

}

添加相应对应强类型视图

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
@model Ninesky.Models.User

@{

ViewBag.Title = "首页";

Layout = "~/Views/Layout/_User.cshtml";

}

<div class="leftnav">这里左侧导航列表</div>

<div class="workspace">

<div class ="Nav">您现在的位置: 用户首页</div>

<div>@Model.UserName

<br />

@Model.GroupId

</div>

</div>

现在要把左侧导航列表做出来,在视图的User文件夹上点右键新建局部视图PartialPersonalNav

MVC4制作网站教程第二章 用户修改资料2.4

?

1

2

3

4

5

6
<ul>

<li>@Html.ActionLink("用户首页","Default","User")</li>

<li>@Html.ActionLink("修改信息","ChangeInfo","User")</li>

<li>@Html.ActionLink("修改密码","ChangePassword","User")</li>

<li>@Html.ActionLink("退出系统","Logout","User")</li>

</ul>

将default.cshtml中“这里左侧导航列表”替换为@Html.Partial("PartialPersonalNav")。浏览器中打开,导航列表显示出来了。

MVC4制作网站教程第二章 用户修改资料2.4

现在开始做修改用户资料了。在【UserController】添加[ChangeInfo] action

?

1

2

3

4

5

6

7
[UserAuthorize]

public ActionResult ChangeInfo()

{

userRsy = new UserRepository();

var _user = userRsy.Find(UserName);

return View(_user);

}

添加修改资料的处理 action

  1. [HttpPost]
  2. [UserAuthorize]
  3. publicActionResultChangeInfo(Useruser)
  4. {
  5. userRsy=newUserRepository();
  6. if(userRsy.Authentication(UserName,Ninesky.Common.Text.Sha256(user.Password))==0)
  7. {
  8. var_user=userRsy.Find(UserName);
  9. _user.Gender=user.Gender;
  10. _user.Email=user.Email;
  11. _user.QQ=user.QQ;
  12. _user.Tel=user.Tel;
  13. _user.Address=user.Address;
  14. _user.PostCode=user.PostCode;
  15. if(userRsy.Update(_user))
  16. {
  17. Notice_n=newNotice{Title="修改资料成功",Details="您已经成功修改资料!",DwellTime=5,NavigationName="用户首页",NavigationUrl=Url.Action("Default","User")};
  18. returnRedirectToAction("UserNotice","Prompt",_n);
  19. }
  20. else
  21. {
  22. Error_e=newError{Title="修改资料失败",Details="在修改用户资料时时,更新的资料未能保存到数据库",Cause="系统错误",Solution=Server.UrlEncode("<li>返回<ahref='"+Url.Action("ChangeInfo","User")+"'>修改资料</a>页面,输入正确的信息后重新操作</li><li>联系网站管理员</li>")};
  23. returnRedirectToAction("UserError","Prompt",_e);
  24. }
  25. }
  26. else
  27. {
  28. ModelState.AddModelError("Password","密码错误!");
  29. returnView();
  30. }
  31. }

aciton上右键添加强类型视图,修改视图里自动生成代码,完成后。如下:

MVC4制作网站教程第二章 用户修改资料2.4

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107
@model Ninesky.Models.User

@{

ViewBag.Title = "修改个人资料";

Layout = "~/Views/Layout/_User.cshtml";

}

<div class="leftnav">@Html.Partial("PartialPersonalNav")</div>

<div class="workspace">

<div class="Nav">您现在的位置: 用户首页</div>

<div>

@using (Html.BeginForm())

{

@Html.ValidationSummary(true)

<fieldset>

<legend>修改资料</legend>

@Html.HiddenFor(model => model.UserId)

<ul>

<li>

<div class="editor-label">

@Html.LabelFor(model => model.UserName)

</div>

<div class="editor-field">

@Html.DisplayFor(model => model.UserName)

</div>

</li>

<li>

<div class="editor-label">

@Html.LabelFor(model => model.Password)

</div>

<div class="editor-field">

@Html.Password("Password")

@Html.ValidationMessageFor(model => model.Password)

输入正确的密码才能修改资料。

</div>

</li>

<li>

<div class="editor-label">

@Html.LabelFor(model => model.Gender)

</div>

<div class="editor-field">

@Html.RadioButtonFor(model => model.Gender, 0) 男

@Html.RadioButtonFor(model => model.Gender, 1) 女

@Html.RadioButtonFor(model => model.Gender, 2) 保密

</div>

</li>

<li>

<div class="editor-label">

@Html.LabelFor(model => model.Email)

</div>

<div class="editor-field">

@Html.EditorFor(model => model.Email)

@Html.ValidationMessageFor(model => model.Email)

@Html.DisplayDescriptionFor(model => model.Email)

</div>

</li>

<li>

<div class="editor-label">

@Html.LabelFor(model => model.QQ)

</div>

<div class="editor-field">

@Html.EditorFor(model => model.QQ)

@Html.ValidationMessageFor(model => model.QQ)

@Html.DisplayDescriptionFor(model => model.QQ)

</div>

</li>

<li>

<div class="editor-label">

@Html.LabelFor(model => model.Tel)

</div>

<div class="editor-field">

@Html.EditorFor(model => model.Tel)

@Html.ValidationMessageFor(model => model.Tel)

@Html.DisplayDescriptionFor(model => model.Tel)

</div>

</li>

<li>

<div class="editor-label">

@Html.LabelFor(model => model.Address)

</div>

<div class="editor-field">

@Html.EditorFor(model => model.Address)

@Html.ValidationMessageFor(model => model.Address)

@Html.DisplayDescriptionFor(model => model.Address)

</div>

</li>

<li>

<div class="editor-label">

@Html.LabelFor(model => model.PostCode)

</div>

<div class="editor-field">

@Html.EditorFor(model => model.PostCode)

@Html.ValidationMessageFor(model => model.PostCode)

@Html.DisplayDescriptionFor(model => model.PostCode)

</div>

</li>

<li><input type="submit" value="修改" /></li>

</ul>

</fieldset>

}

</div>

</div>

@section Scripts {

@Scripts.Render("~/bundles/jqueryval")

}

运行一下看

MVC4制作网站教程第二章 用户修改资料2.4

输入资料测试一下。能够正常保存到数据库。

======================================
刚开始学MVC,加之表达能力有限,也没有事先写个大致计划,写的很差、很乱。现在是想到哪里就写哪里,有时候做到后面了,发现前边写的不行又去改前面写的代码。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://www.cnblogs.com/mzwhj/archive/2012/11/06/2757152.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 MVC4制作网站教程第二章 用户修改资料2.4 https://www.kuaiidc.com/100143.html

相关文章

发表评论
暂无评论