我正在开发一个ASP.NET MVC4应用程序。我实际上希望能够更改用户的电子邮件地址。
ModifyEmail.cshtml
@model Project.Models.ModifyEmailModel
@using (Html.BeginForm("Manage", "Account")) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Change Email Address</legend>
<ol>
<li>
@Html.LabelFor(m => m.OldEmailAddress)
@Html.EditorFor(m => m.OldEmailAddress)
@Html.ValidationMessageFor(m => m.OldEmailAddress)
</li>
<li>
@Html.LabelFor(m => m.NewEmailAddress)
@Html.EditorFor(m => m.NewEmailAddress)
@Html.ValidationMessageFor(m => m.NewEmailAddress)
</li>
<li>
@Html.LabelFor(m => m.ConfirmEmailAddress)
@Html.TextBoxFor(m => m.ConfirmEmailAddress)
@Html.ValidationMessageFor(m => m.ConfirmEmailAddress)
</li>
</ol>
<input type="submit" value="Changer l'addresse email" />
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
AccountModels.cs
namespace Project.Models
{
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
}
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string EmailId { get; set; }
public string Details { get; set; }
}
public class ModifyEmailModel
{
[Required(ErrorMessage = "l'addresse email actuelle est requise")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Addresse email actuelle")]
public string OldEmailAddress { get; set; }
[Required(ErrorMessage = "la nouvelle addresse email est requise")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Nouvelle addresse email")]
public string NewEmailAddress { get; set; }
[DataType(DataType.EmailAddress)]
[Display(Name = "Confirmer la nouvelle addresse mail")]
[Compare("NewEmailAddress", ErrorMessage = "Le nouvelle addresse email et la confirmation de l'addresse email ne correspondent pas.")]
public string ConfirmEmailAddress { get; set; }
}
}
我无法弄清楚如何在AccountController.cs中实现该操作,请帮忙!
答案 0 :(得分:1)
我从您的描述中了解到,您需要一种操作方法来修改电子邮件地址。所以,你可以试试以下,
[HttpPost]
public ActionResult Manage(ModifyEmailModel data)
{
if(data.NewEmailAddress == data.ConfirmEmailAddress)
{
// db is your context instance
var u = db.UserProfiles.Where(x => x.EmailId == OldEmailAddress).SingleOrDefault();
u.EmailID = data.NewEmailAddress;
db.SaveChanges();
}
else
string error = "Email ID's don't match";
return View();
}
要在您的视图中以GET方式显示电子邮件,即加载视图的方法,请输入以下内容,
public ActionResult Manage()
{
// at this point you must have the username for your currently logged in user
var u = db.UserProfiles.Where(x => x.UserName == UserName).SingleOrDefault();
return View(u);
}
答案 1 :(得分:0)
默认身份帐户控制器使用“管理”操作更新或创建密码吗?如果你想保留这个,我建议你为ManageEmail这样的东西创建另一个GET动作,在那里你可以通过原始管理页面下的链接或按钮点击返回你刚创建的视图。然后创建一个POST操作ManageEmail以使用新电子邮件执行更改电子邮件/更新异步。
在Manage.cshtml中,您可以使用Html.Action调用其中的不同get方法。
@Html.Partial("_ChangePassword") // change password partial here
@Html.Action("ManageEmail") // this will call the partial view page with your custom model
在AccountController.cs中添加以下两种方法:
//GET ManageEmail
public ActionResult ManageEmail()
{
var model = new ModifyEmailModel()
{
OldEmailAddress = UserManager.GetEmail(User.Identity.GetUserId()) // this will load the current email for your view
};
return View("_ManageEmail",model); // _ManageEmail is your partial view name
}
//POST ManageEmail
[HttpPost()]
[ValidateAntiForgeryToken()]
public async Task<ActionResult> ManageEmail(ModifyEmailModel data)
{
//some validation here like if email is valid or not existing in the database to avoid duplicate
var user = UserManager.FindById(User.Identity.GetUserId());
await UserManager.UpdateAsync(user);
return RedirectToAction("Manage");
}
然后只需编辑部分视图以定位ManageEmail POST操作。