2017-08-28 74 views
1

我遇到了一個小問題,例如 - 使用實體框架編輯模型屬性。使用Identity2編輯用戶信息

1)因此,讓我們開始吧 - 我想編輯屬性 「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)並試圖定義我的 「EditInfoMethod」`的觀點:

@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)和N主要問題:我寫地址欄地址應該通過GET方法 - 但我一直捕捉這個異常: Exception

5)如果你可以看到我的數據庫 - 這個電子郵件已經創建並應該存在:User database

6)總結 - 我嘗試過不同的方法來正確地更改「EditPacientInfo」,但我無法通過互聯網做出或找到​​正確的決定。 我會很高興,如果你能找到/寫一些東西,真的可以幫助我在這種情況下(謝謝你們所有人(並抱歉不太好英語以及^^))

+0

有些事情讓你試穿: 1)在HTTPGET行動,改變'字符串email'到'字符串id' 2)添加[FromRoute]像'公衆的ActionResult EditPacientInfo( [FromRoute]字符串電子郵件)' 3)將您的電子郵件地址:[email protected]編碼爲aaa%40bbb.com.br 問候。 – dime2lo

+0

謝謝,很多人,我發現一些有用的答案,幫助我解決這個問題: https://stackoverflow.com/questions/22955872/editing-user-profile-details https://stackoverflow.com/questions/ 24343246/asp-net-identity-multiple-object-sets-per-type-are-not-supported - 如果你對Identity 2數據庫有問題 - >這將解決自動腳手架問題 – clyde

回答

0

我認爲URL輸入在瀏覽器中的格式不正確。試試以下網址:

localhost:1290/Doctor/[email protected]@gmail.com