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:
5) If you can see in my database - this e-mail is already created and supposed to exist:
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^^))