1

I got stuck with a little problem like - editing model properties using Entity Framework.

1)So,let's begin - I want to edit 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) And i added some basic logic to edit this property : GET+POST Method

[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) And tried to customize my "EditInfoMethod"`s view :

    @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) And now the main problem: I write in address bar address that supposed to get through the GET method - but I keep catching this exception: Exception

5) If you can see in my database - this e-mail is already created and supposed to exist: User database

6) To conclude - I've tried different methods to change "EditPacientInfo" properly, but I cannot make or find right decision through the Internet. I will be glad if you could find/write some stuff that could really help me in this situation(thanks to all of you(and sorry for not so good English as well^^))

TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
clyde
  • 121
  • 4
  • Some things for you to try on: 1) In the HttpGet action, change `string email` to `string id` 2) Add [FromRoute] like `public ActionResult EditPacientInfo([FromRoute] string email)` 3) Encode your e-mail: aaa@bbb.com to aaa%40bbb.com.br Regards. – dime2lo Aug 28 '17 at 21:47
  • Thanks ,A lot everyone i found some useful answers that helped me to solve this problem : 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 - if you have problem with Identity 2 database -> this will solve problem with auto-scaffolding – clyde Aug 29 '17 at 16:59

1 Answers1

0

I think URL that entered in the browser is not in the correct format. Try following URL :

localhost:1290/Doctor/EditPacientInfo?email=test@pacient@gmail.com
Alsamil Mehboob
  • 384
  • 2
  • 6
  • 24