编辑用户时出错[ASP MVC]

时间:2016-02-19 23:28:31

标签: asp.net-mvc asp.net-mvc-4 razor

编辑用户信息时遇到问题,当我尝试编辑昵称或电子邮件或电话号码时,点击保存,没有任何更改......

这是我的帐户控制器方法:

[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; }

}

任何人都可以帮我找到问题并感谢

1 个答案:

答案 0 :(得分:0)

解决这个问题(谢谢@ Stephen-Muecke):

  

ModelState实际上是否有效(并且您点击代码来保存数据)?控制器方法的最后一行应该返回View(model); (不返回RedirectToAction(“Index”);)以便显示验证错误并进行纠正。

public ActionResult Edit_Info(UserVm model)
{
    var id = User.Identity.GetUserId();

    if (id != null)
    {
        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 view (model);

}