0
我有修改用戶信息,當我嘗試編輯暱稱或電子郵件地址或電話號碼,然後點擊保存,什麼都沒有改變一個問題....錯誤與編輯用戶[ASP MVC]
這裏是我的賬戶控制器方法:
[Authorize]
[HttpPost]
// POST: /Account/Edit_Info
public ActionResult Edit_Info(UserVm model)
{
var id = User.Identity.GetUserId();
if (ModelState.IsValid)
{
var User_Model = db.AspNetUsers.Find(id);
User_Model.NickName = model.User.NickName;
User_Model.Email = model.User.Email;
User_Model.PhoneNumber = model.User.PhoneNumber;
db.Entry(User_Model).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}
這是我的觀點:
@using (Html.BeginForm("Edit_Info", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Personal Information</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.User.Id)
<div class="form-group">
@Html.LabelFor(model => model.User.NickName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.User.NickName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.User.NickName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.User.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.User.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.User.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.User.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.User.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.User.PhoneNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn green" />
</div>
</div>
</div>
}
這裏是我的ViewModel:
public class UserVm
{
public List<Book> Books { set; get; }
public AspNetUser User { set; get; }
public List<ReadLater> ReadLaters { set; get; }
public List<Favourite> Favourites { set; get; }
public ChangePasswordViewModel ChangePasswordViewModel { set; get; }
}
誰能幫我找到這個問題,並感謝
是'ModelState'實際上是有效的(和你打的代碼來保存數據)?控制器方法的最後一行應該是'返回View(model);'(不返回RedirectToAction(「Index」);'),以便驗證錯誤顯示並且可以被糾正。 –
其實你是對的,已經解決了@StephenMuecke –