MVC4制作网站教程第二章 用户修改资料2.4
程序员文章站
2023-12-04 22:03:16
一、用户
1.1用户注册
1.2用户登录
1.3修改密码
1.4修改资料
在用户登陆成功后要跳转到...
一、用户
1.1用户注册
1.2用户登录
1.3修改密码
1.4修改资料
在用户登陆成功后要跳转到一个页面,暂且叫做用户中心吧。在【usercontroller】添加[default] action
[userauthorize] public actionresult default() { userrsy = new userrepository(); var _user = userrsy.find(username); return view(_user); }
添加相应对应强类型视图
@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
<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")。浏览器中打开,导航列表显示出来了。
现在开始做修改用户资料了。在【usercontroller】添加[changeinfo] action
[userauthorize] public actionresult changeinfo() { userrsy = new userrepository(); var _user = userrsy.find(username); return view(_user); }
添加修改资料的处理 action
[httppost] [userauthorize] public actionresult changeinfo(user user) { userrsy = new userrepository(); if(userrsy.authentication(username,ninesky.common.text.sha256(user.password))==0) { var _user = userrsy.find(username); _user.gender = user.gender; _user.email = user.email; _user.qq = user.qq; _user.tel = user.tel; _user.address = user.address; _user.postcode = user.postcode; if (userrsy.update(_user)) { notice _n = new notice { title = "修改资料成功", details = "您已经成功修改资料!", dwelltime = 5, navigationname = "用户首页", navigationurl = url.action("default", "user") }; return redirecttoaction("usernotice", "prompt", _n); } else { error _e = new error { title = "修改资料失败", details = "在修改用户资料时时,更新的资料未能保存到数据库", cause = "系统错误", solution = server.urlencode("<li>返回<a href='" + url.action("changeinfo", "user") + "'>修改资料</a>页面,输入正确的信息后重新操作</li><li>联系网站管理员</li>") }; return redirecttoaction("usererror", "prompt", _e); } } else { modelstate.addmodelerror("password","密码错误!"); return view(); } }
aciton上右键添加强类型视图,修改视图里自动生成代码,完成后。如下:
@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") }
运行一下看
输入资料测试一下。能够正常保存到数据库。
======================================
刚开始学mvc,加之表达能力有限,也没有事先写个大致计划,写的很差、很乱。现在是想到哪里就写哪里,有时候做到后面了,发现前边写的不行又去改前面写的代码。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。