使用Identity2编辑用户信息

时间:2017-08-28 21:13:21

标签: c# asp.net-mvc entity-framework asp.net-identity owin

我遇到了一个小问题 - 比如使用Entity Framework编辑模型属性。

1)所以,让我们开始吧 - 我想编辑Property" PacientInfo"

 public class RegisterViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Адрес электронной почты")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "Значение {0} должно содержать не менее {2} символов.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Пароль")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Подтверждение пароля")]
    [Compare("Password", ErrorMessage = "Пароль и его подтверждение не совпадают.")]
    public string ConfirmPassword { get; set; }

    public string Name { get; set; }

    public string PacientInfo { get; set; }
}

2)我添加了一些基本逻辑来编辑这个属性:      GET + POST方法

[HttpGet]
    public ActionResult EditPacientInfo(string email)
    {
        var UserManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        ApplicationUser appUser = new ApplicationUser();
        appUser = UserManager.FindByEmail(email);
        PacientEdit user = new PacientEdit()
        {
            Email = appUser.Email,
            PacientInfo = appUser.PacientInfo
        };
        if (email== null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        ApplicationUser pacient = db.Users.Find(email);

        if (pacient == null)
        {
            return HttpNotFound();
        }
        return View(pacient);
    }
 [HttpPost]
    public ActionResult EditPacientInfo(ApplicationUser model)
    {

        var UserManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        if (ModelState.IsValid)
        {
            ApplicationUser u = UserManager.FindById(model.Id);
            u.Email = model.Email;
            u.PacientInfo= model.PacientInfo; // Extra Property
            UserManager.Update(u);
            return RedirectToAction("Index");
        }
        return View(model);
    }

3)并尝试自定义我的&#34; EditInfoMethod&#34;的视图:

    @model med_projec_roles_added.Models.RegisterViewModel
@{
    ViewBag.Title = "EditPacientInfo";
}
@model med_projec_roles_added.Models.ApplicationUser
<h2>Pacient ,  @Model.Email</h2>
@using (Html.BeginForm())
{

            @Html.LabelFor(model => model.PacientInfo, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PacientInfo)
                @Html.ValidationMessageFor(model => model.PacientInfo)
            </div>
}

4)现在主要的问题是:我写入了应该通过GET方法的地址栏地址 - 但我一直在捕捉这个异常: Exception

5)如果你能在我的数据库中看到 - 这封电子邮件已经创建并且应该存在:User database

6)总结一下 - 我尝试过不同的方法来改变&#34; EditPacientInfo&#34;正确,但我无法通过互联网做出正确的决定。 如果你能找到/写出一些在这种情况下可以真正帮助我的东西,我将很高兴(感谢你们所有人(对不起那么好的英语也很好^^))

1 个答案:

答案 0 :(得分:0)

我认为在浏览器中输入的URL格式不正确。请尝试以下网址:

localhost:1290/Doctor/EditPacientInfo?email=test@pacient@gmail.com
相关问题