MVC4制作网站教程第三章 添加用户组操作3.2
程序员文章站
2023-12-04 22:03:10
一、用户
二、用户组
2.1浏览用户组
2.2添加用户组
修改[add]action
///
/// 添加用户组...
一、用户
二、用户组
2.1浏览用户组
2.2添加用户组
修改[add]action
/// <summary> /// 添加用户组 /// </summary> /// <returns></returns> [adminauthorize] public actionresult add() { viewdata.add("type", typeselectlist); return view(); }
添加强类型视图
完成后代码如下:
@model ninesky.models.usergroup@{ viewbag.title = "添加用户组"; layout = "~/views/layout/_manage.cshtml"; } <div class="left"> <div class="top"></div> 左侧列表 </div> <div class="split"></div> <div class="workspace"> <div class="inside"> <div class="notebar"> <img alt="" src="~/skins/default/manage/images/usergroup.gif" />添加用户组 </div> @using (html.beginform()) { @html.validationsummary(true) <fieldset> <legend>用户组资料</legend> @html.hiddenfor(model => model.usergroupid) <ul> <li> <div class="editor-label"> @html.labelfor(model => model.type) </div> <div class="editor-field"> @html.dropdownlist("type") </div> </li> <li> <div class="editor-label"> @html.labelfor(model => model.name) </div> <div class="editor-field"> @html.editorfor(model => model.name) @html.validationmessagefor(model => model.name) </div> </li> <li> <div class="editor-label"> @html.labelfor(model => model.description) </div> <div class="editor-field"> @html.editorfor(model => model.description) @html.validationmessagefor(model => model.description) </div> </li> <li> <div class="editor-label"> </div> <div class="editor-field"> <input type="submit" value="保存" /> </div> </li> </ul> </fieldset> } </div> </div> <div class="clear"></div> @section scripts { @scripts.render("~/bundles/jqueryval") }
修改用户组添加处理动作[add(usergroup usergroup)]action,完成后的代码
[httppost] [adminauthorize] public actionresult add(usergroup usergroup) { usergrouprsy = new usergrouprepository(); if (usergrouprsy.add(usergroup)) { notice _n = new notice { title = "添加用户组成功", details = "您已经成功添加["+usergroup.name+"]用户组!", dwelltime = 5, navigationname = "用户组列表", navigationurl = url.action("list", "usergroup") }; return redirecttoaction("managenotice", "prompt", _n); } else { error _e = new error { title = "添加用户组失败", details = "在添加用户组时,未能保存到数据库", cause = "系统错误", solution = server.urlencode("<li>返回<a href='" + url.action("add", "usergroup") + "'>添加用户</a>页面,输入正确的信息后重新操作</li><li>联系网站管理员</li>") }; return redirecttoaction("manageerror", "prompt", _e); } }
浏览器中查看一下
输入资料测试一下,可以添加资料。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。