主要完成一个功能是后台管理员添加用户,用户有分组,因此建立了一个model,代码下:
[NotMapped] [Bind(Include = "UserName,LoginName,RoleId")] public class AddUser { [DisplayName("帐号")] [Required(ErrorMessage = "请输入帐号")] [MaxLength(20, ErrorMessage = "帐号长度不得超过20个字,此帐号是作为人大代表登录系统的帐号。")] //[Remote("CheckDup", "PeoplesCongress", HttpMethod = "POST", ErrorMessage = "你输入的帐号已有人用了!")] public string LoginName { get; set; } [DisplayName("姓名")] [Required(ErrorMessage = "请输入姓名")] [MaxLength(24, ErrorMessage = "中文姓名不可超过12个汉字")] public string UserName { get; set; } /// <summary> /// 密码 /// </summary> [StringLength(20, MinimumLength = 6, ErrorMessage = "×")] [DataType(DataType.Password)] public string Password { get; set; } [Display(Name = "用户组", Description = "请选择用户组。")] [Required(ErrorMessage = "×")] [StringLength(1, MinimumLength = 1, ErrorMessage = "×")] public int RoleId { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public DateTime RegisterOn { get; set; } public AddUser GetUser() { return new AddUser { LoginName = this.LoginName, UserName = this.UserName, RoleId = this.RoleId }; } }
contorlles里代码如下:
public ActionResult AddUser() { ViewData["RoleId"] = new SelectList(dbContext.Roles, "RoleId", "RoleName"); return View(); } // // POST: AddUser [HttpPost] // [AdminAuthorize] public ActionResult AddUser(AddUser adduser) { if (ModelState.IsValid) { if (dbContext.Set<User>().Any(u => u.LoginName == adduser.LoginName)) ModelState.AddModelError("LoginName", "登录名已存在"); User _user = new User(); _user.Password = Common.Text.Sha256("longhua").ToString(); _user.RegisterOn = System.DateTime.Now; _user.LoginName = adduser.LoginName; _user.UserName = adduser.UserName; _user.RoleId = Int32.Parse(adduser.RoleId.ToString()); // adduser.RoleId; dbContext.Users.Add(_user); } //return Json(User, JsonRequestBehavior.AllowGet); return View("Index"); }
View中代码:
<div class="form-horizontal"> <dl> <dt>添加用户</dt> <dd> <div class="label">@Html.LabelFor(model => model.LoginName):</div> <div class="ctrl"> @Html.EditorFor(model => model.LoginName) @Html.ValidationMessageFor(model => model.LoginName) @Html.DisplayDescriptionFor(model => model.LoginName) </div> </dd> <dd> <div class="label">@Html.LabelFor(model => model.UserName):</div> <div class="ctrl"> @Html.EditorFor(model => model.UserName) @Html.ValidationMessageFor(model => model.UserName) @Html.DisplayDescriptionFor(model => model.UserName) </div> </dd> <dd> <div class="label">@Html.LabelFor(model => model.RoleId):</div> <div class="ctrl"> @*<select id="RoleId" name="RoleId"> <option value="0">请选择</option> <option value="1">人大代表组</option> <option value="2">提案管理员组</option> <option value="3">系统管理员组</option> </select>*@ @Html.DropDownList("RoleId", "请选择用户角色") </div> </dd> <dd> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="保存" class="btn btn-default" id="btnadd" name="btnadd" /> </div> </div> </dd> </dl> </div>
数据验证都没错,但是提交后,系统报错,提示不能将System.Int32强制转换为System.string...在园子里找了好久,也没找到原因,请大神们帮忙看看。
前台要用@Html.DropDownListFor(model => model.LeaderID, ViewBag.UserListForDrop as SelectList),这样实体才能获得数据,你那样写也可以,用Request["RoleId"]获取。另外后台也可以简洁点
if (dbContext.Set<User>().Any(u => u.LoginName == adduser.LoginName)) ModelState.AddModelError("LoginName", "登录名已存在");
//部分默认数据
adduser.Password = Common.Text.Sha256("longhua").ToString();
adduser.RegisterOn = System.DateTime.Now;
dbContext.Users.Add(adduser);
非常感谢。
请查查@Html.DropDownList("RoleId", "请选择用户角色")生成的html 的value和text是什么?
你的错误主要在_user.RoleId = Int32.Parse(adduser.RoleId.ToString())和view的@Html.DropDownList("RoleId", "请选择用户角色")这个语句。
谢谢。