Hello everyone, I created an identity system using the asp.net mvc template.
Redefining the system and adding fields ...
// Фамилия пользователя public string SecondName { get; set; } // Имя пользователя public string FirstName { get; set; } // Отчество пользователя public string PatronymicName { get; set; } // Дата рождения, необходима для фильтрации игр по возрастной категории public DateTime? BirthDate { get; set; } // Номер телефона public string TelephoneNumber { get; set; } ... by adding tabs to the IndexViewModel nav view ...
<ul class="nav nav-tabs" id="myTab"> <li class="active"> <a href="#mygames" data-toggle="tab">Моя коллекция игр</a> </li> <li> <a href="#mydata" data-toggle="tab">Мои данные</a> </li> </ul> ... I fill the data with content from the partial view "_EditInfo":
<div class="tab-pane" id="mydata"> @Html.Partial("_EditInfo"); </div> _EditInfo.cshtml
@model MyGameStore.Models.EditInfoViewModel @using (Html.BeginForm("EditInfo", "Manage", FormMethod.Post, new { @class = "form-signin form-horizontal", role = "form" })) { @Html.AntiForgeryToken() @Html.ValidationSummary("", new { @class = "text-danger" }) <div class="form-group"> <div class="col-xs-4"> @Html.LabelFor(m => m.SecondName, new { @class = "col-md-2 control-label" }) @Html.TextBoxFor(m => m.SecondName, new { @class = "form-control", value = Model.SecondName }) </div> </div> <div class="form-group"> <div class="col-xs-4"> <div class="col-xs-4"> @Html.LabelFor(m => m.FirstName, new { @class = "col-md-2 control-label" }) @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control", value = Model.FirstName }) </div> </div> <div class="form-group"> <div class="col-xs-4"> @Html.LabelFor(m => m.PatronymicName, new { @class = "col-md-2 control-label" }) @Html.TextBoxFor(m => m.PatronymicName, new { @class = "form-control", value = Model.PatronymicName }) </div> </div> <div class="form-group"> <div class="col-xs-4"> @Html.LabelFor(m => m.BirthDate, new { @class = "col-md-2 control-label" }) @Html.TextBoxFor(m => m.BirthDate, new { @class = "form-control", value = Model.BirthDate }) </div> </div> <div class="form-group"> <div class="col-xs-4"> @Html.LabelFor(m => m.TelephoneNumber, new { @class = "col-md-2 control-label" }) @Html.TextBoxFor(m => m.TelephoneNumber, new { @class = "form-control", value = Model.TelephoneNumber }) </div> </div> <div class="form-group"> <div class="col-xs-4"> <br /> <button class="btn btn-primary btn-block" type="submit"><i class="fa fa-save"></i> Применить</button> <button class="btn btn-default btn-block" type="reset"><i class="fa fa-repeat"></i> Сбросить</button> </div> </div> </div> } This partial view is just a post-form that allows you to change information about the user of the Identity system.
Now, in the ManageController controller, I respectively have GET and POST actions with the EditInfoViewModel model, which contains exactly the same fields that I redefined in the Identity system.
GET: EditInfo
public async Task<ActionResult> EditInfo() { var user = await UserManager.FindByIdAsync(User.Identity.GetUserId()); if (user == null) return HttpNotFound(); var model = new EditInfoViewModel { SecondName = user.SecondName, FirstName = user.FirstName, PatronymicName = user.PatronymicName, BirthDate = user.BirthDate, TelephoneNumber = user.TelephoneNumber }; return PartialView("_EditInfo", model); } POST: EditInfo
[HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> EditInfo([Bind(Include = "SecondName,FirstName,PatronymicName,BirthDate,TelephoneNumber")] EditInfoViewModel model) { var user = await UserManager.FindByEmailAsync(User.Identity.Name); if (!ModelState.IsValid) return PartialView("_EditInfo", model); if (user != null) { user.SecondName = model.SecondName; user.FirstName = model.FirstName; user.PatronymicName = model.PatronymicName; user.BirthDate = model.BirthDate; user.TelephoneNumber = model.TelephoneNumber; var result = await UserManager.UpdateAsync(user); if (result.Succeeded) return RedirectToAction("Index"); ModelState.AddModelError("", "Что-то пошло не так"); } else ModelState.AddModelError("", "Пользователь не найден"); return PartialView("_EditInfo", model); } Debugging problem
When rendering a partial view @Html.Partial("_EditInfo"); in the Index (Manage) view, a conflict arises between the two models "IndexViewModel" and "EditInfoViewModel":
The model element passed to the dictionary is of the type "MyGameStore.Models.IndexViewModel", but this dictionary requires a model element of the type "MyGameStore.Models.EditInfoViewModel".