冯 海

一个程序新人菜鸟的日记,希望大家多多关照。QQ:32316131

ASP.NET Identity教程二:(用户管理)2

4. 编辑用户

编辑用户是指根据用户的唯一 Id 值获取某单个用户的信息,然后在页面上将需要修改的字段值显示在可编辑的控件中,供用户修改使用。

4.1. 编写 EditUserViewModel

在“ViewModels”文件夹中添加一个名称为“EditUserViewModel”的类,然后编写如
下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace jsdhh2.ViewModels
{
	public class EditUserViewModel
	{
		[Display(Name = "用户名")]
		public string UserName { get; set; }


		[Display(Name = "电话(手机/固话)")]
		[Phone]
		public string PhoneNumber { get; set; }

		[Required]
		[EmailAddress]
		[Display(Name = "电子邮件")]
		public string Email { get; set; }


		#region 添加字段
		[Display(Name = "微信")]
		public virtual string WX { get; set; }

		public virtual string QQ { get; set; }
		[Display(Name = "创建时间")]
		public virtual DateTime CreateTime { get; set; }
		[Display(Name = "部门ID")]
		public virtual int? DepartmentId { get; set; }
		[Display(Name = "地址")]
		public virtual string Address { get; set; }
		[Display(Name = "性别")]

		public virtual int? Gender { get; set; }
		[Display(Name = "生日")]
		public virtual DateTime? BirthDate { get; set; }
		[Display(Name = "出生时间")]
		public virtual string TheHour { get; set; }
		[Display(Name = "详细生日")]
		public virtual DateTime? DetailedTime { get; set; }
		[Display(Name = "真实姓名")]

		public virtual string RealName { get; set; }
		[Display(Name = "配偶ID")]

		public virtual int? SpouseId { get; set; }
		[Display(Name = "头像")]
		public virtual string HeaderPic { get; set; }


		#endregion
	}


}

 

这里没有关于密码的属性,密码一般情况下会单独做成一个页面,用于在用户丢失密码的情况下修改。这里只修改用户的基本信息。

 4.2. 编写 Edit 方法

在 UserController 控制器中添加一个带有 HttpGet 特性的 Edit()方法,用于呈现编辑用户的表单信息视图。再添加一个带有 HttpPost 特性的 Edit()方法,用于提交修改后的用户信息到数据库中。完整的代码如下:

		[HttpGet]
		public ActionResult Edit(string id)
		{
			if (string.IsNullOrWhiteSpace(id))
			{
				return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
			}
			User user = UserManager.FindById(id);
			if (user == null)
			{
				return HttpNotFound();
			}
			var editUserViewModel = new EditUserViewModel()
			{
				Email = user.Email,
				PhoneNumber = user.PhoneNumber,
				WX=user.WX,
				QQ=user.QQ,
				DepartmentId=user.DepartmentId,
				Address=user.Address,
				Gender= user.Gender,
				RealName=user.RealName,
				SpouseId=user.SpouseId,
				HeaderPic=user.HeaderPic,
				BirthDate=user.BirthDate,
				TheHour=user.TheHour,
				DetailedTime=user.DetailedTime
			};
			return View(editUserViewModel);
		}

		[HttpPost]
		[ValidateAntiForgeryToken]
		public async Task<ActionResult> Edit(string id, EditUserViewModel editUserViewModel)
		{
			if (ModelState.IsValid && !string.IsNullOrEmpty(id))
			{
				User user = UserManager.FindById(id);
				user.Email = editUserViewModel.Email;
				user.PhoneNumber = editUserViewModel.PhoneNumber;
				user.WX = editUserViewModel.WX;
				user.QQ = editUserViewModel.QQ;
				user.DepartmentId = editUserViewModel.DepartmentId;
				user.Address = editUserViewModel.Address;
				user.Gender = editUserViewModel.Gender;
				user.RealName = editUserViewModel.RealName;
				user.SpouseId = editUserViewModel.SpouseId;
				user.HeaderPic = editUserViewModel.HeaderPic;
				user.BirthDate = editUserViewModel.BirthDate;
				user.TheHour = editUserViewModel.TheHour;
				user.DetailedTime = editUserViewModel.DetailedTime;
			
				var result = await UserManager.UpdateAsync(user);
				if (result.Succeeded)
				{
					return RedirectToAction("List", "User");
				}
				AddErrors(result);
			}
			return View(editUserViewModel);
		}

  

这里使用了 UserManager 类中的 UpdateAsync()异步方法来更新一个用户信息。在执行更新时,首先要获取要更新的 User 对象,然后再在此对象上修改其中的属性值。

4。3. 编辑 Edit 视图

”项目的“Views”“User”中添加一个名称为“Edit”的视图

“Edit.cshtml”视图中编写如下代码:注意,我加入了一个laydate日历控件,感觉好方便,很好用,这个作者技术大牛呀。laydate官网http://www.layui.com/laydate/

    

@model jsdhh2.ViewModels.EditUserViewModel
@{
	ViewBag.Title = "Edit";


}

@using (Html.BeginForm("Edit", "User", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
	@Html.AntiForgeryToken()
	<h4>编辑用户资料。</h4>
	<hr />


	<div class="form-group">
		<a class="btn btn-primary" href="/User/List">返回</a>
		<a class="btn btn-success" href="/User/List">上传头像</a>
		<a class="btn btn-warning" href="/User/List">配偶</a>
		<a class="btn btn-primary" href="/User/List">修改密码</a>
	</div>


	<div class="form-group">
		<img src="@Model.HeaderPic" class = "col-md-2 control-label" />
		 
	</div>


	<div class="form-group">
		@Html.LabelFor(m => m.RealName, new { @class = "col-md-2 control-label" })
		<div class="col-md-10">
			@Html.TextBoxFor(m => m.RealName, new { @class = "form-control" })
		</div>
	</div>
	<div class="form-group">
		@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
		<div class="col-md-10">
			@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
		</div>
	</div>
	<div class="form-group">
		@Html.LabelFor(m => m.PhoneNumber, new { @class = "col-md-2 control-label" })
		<div class="col-md-10">@*  *@
			@Html.TextBoxFor(m => m.PhoneNumber, new { @class = "form-control" })
		</div>
	</div>
	<div class="form-group">
		@Html.LabelFor(m => m.BirthDate, new { @class = "col-md-2 control-label" })
		<div class="col-md-10">
			 <input type="datetime" class="laydate-icon" id="demo" value=@Model.BirthDate  name="BirthDate"/>
			@*@Html.TextBoxFor(m => m.BirthDate, "{0:yyyy-MM-dd  HH:mm:ss}", new { @class = "laydate-icon", id = "demo" })*@
		</div>
	</div>
	<div class="form-group">
		@Html.LabelFor(m => m.TheHour, new { @class = "col-md-2 control-label" })
		<div class="col-md-10">
		
			@Html.DropDownListFor(m => m.TheHour, new List<SelectListItem>()
	   {
		   new SelectListItem() { Text= "23点-凌晨1点", Value = "子时" },
		   new SelectListItem() { Text= "凌晨1点-3点", Value = "丑时" },
			 new SelectListItem() { Text= "凌晨3点-5点", Value = "寅时" },
		   new SelectListItem() { Text= "凌晨5点-7点", Value = "卯时" },
			 new SelectListItem() { Text= "7点-9点", Value = "辰时" },
		   new SelectListItem() { Text= "9点-11点", Value = "巳时" },
			 new SelectListItem() { Text= "午11点-13点", Value = "午时" },
		   new SelectListItem() { Text= "13点-15点", Value = "未时" },
			 new SelectListItem() { Text= "15点-17点", Value = "申时" },
		   new SelectListItem() { Text= "17点-19点", Value = "酉时" },
			 new SelectListItem() { Text= "19点-21点", Value = "戌时" },
		   new SelectListItem() { Text= "21点-23点", Value = "亥时" },
	   })

			
	
	@*@Html.TextBoxFor(m => m.TheHour, new { @class = "form-control" })*@
		</div>
	</div>

	<div class="form-group">
		@Html.LabelFor(m => m.QQ, new { @class = "col-md-2 control-label" })
		<div class="col-md-10">
			@Html.TextBoxFor(m => m.QQ, new { @class = "form-control" })
		</div>
	</div>

	<div class="form-group">
		@Html.LabelFor(m => m.WX, new { @class = "col-md-2 control-label" })
		<div class="col-md-10">
			@Html.TextBoxFor(m => m.WX, new { @class = "form-control" })
		</div>
	</div>
	
	<div class="form-group">
		@Html.LabelFor(m => m.Address, new { @class = "col-md-2 control-label" })
		<div class="col-md-10">
			@Html.TextBoxFor(m => m.Address, new { @class = "form-control" })
		</div>
	</div>

	<div class="form-group">
		@Html.LabelFor(m => m.Address, new { @class = "col-md-2 control-label" })
		<div class="col-md-10">
			@Html.TextBoxFor(m => m.Address, new { @class = "form-control" })
		</div>
	</div>
	<div class="form-group">
		@Html.LabelFor(m => m.DepartmentId, new { @class = "col-md-2 control-label" })
		<div class="col-md-10">
			@Html.TextBoxFor(m => m.DepartmentId, new { @class = "form-control" })
		</div>
	</div>
	<div class="form-group">
		@Html.LabelFor(m => m.Gender, new { @class = "col-md-2 control-label" })
		<div class="col-md-10">
			@Html.DropDownListFor(m => m.Gender,  new List<SelectListItem>()
	   {
		   new SelectListItem() { Text= "男", Value = "1" },
		   new SelectListItem() { Text= "女", Value = "2" },

	   }) 
			 


				@*@Html.TextBoxFor(m => m.Gender, new { @class = "form-control" })*@
</div>
	</div>



	@Html.ValidationSummary("", new { @class = "text-danger" })

	<div class="form-group">
		<div class="col-md-offset-2 col-md-10">
			<input type="submit" class="btn btn-default" value="提交" />
		</div>
	</div>
}

<script src="~/Content/laydate/laydate.js"></script>

<script>
	; !function () {

		//laydate.skin('molv');

		laydate({
			elem: '#demo',
			istime: true,
			format: 'YYYY-MM-DD hh:mm:ss'


		})

	}();
</script>

  

 

   注意,这里不必用input来弄日期控件,textbox不行。

 注意这里的下拉菜单。因为我当时坑爹的当性别设为int?,如是男是1,女是2.可这个下拉是string的,所以在后台得进行转换。

if (ModelState.IsValid && !string.IsNullOrEmpty(id))
			{


				
				User user = UserManager.FindById(id);
		     	var	_intGender = Convert.ToInt32(editUserViewModel.Gender);

				user.Email = editUserViewModel.Email;
				user.PhoneNumber = editUserViewModel.PhoneNumber;
				user.WX = editUserViewModel.WX;
				user.QQ = editUserViewModel.QQ;
				user.DepartmentId = editUserViewModel.DepartmentId;
				user.Address = editUserViewModel.Address;
				user.Gender =_intGender;
				user.RealName = editUserViewModel.RealName;
				user.SpouseId = editUserViewModel.SpouseId;
				user.BirthDate = editUserViewModel.BirthDate;
				user.TheHour = editUserViewModel.TheHour;
				
			
				var result = await UserManager.UpdateAsync(user);
				if (result.Succeeded)
				{

  下面我得将时辰的下拉也做好,这样就帖上完整 的Edit的代码

 

posted @ 2017-05-13 22:00  秋天来了哟  阅读(403)  评论(2编辑  收藏  举报
认识就是缘份,愿天下人都快乐!
QQ: 32316131
Email: 32316131@qq.com