编辑用户个人资料详细信息

编辑用户个人资料详细信息

本文介绍了编辑用户个人资料详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建用于编辑用户自定义信息的操作和视图?

How to create action and views for editing user custom informations?

授权基于VS使用MVC 4项目创建的成员资格.

Authorization is based on membership created by VS with MVC 4 project.

我添加了其他列,例如FirstName等.我需要并且注册可以正常运行,但是我不知道如何获取此属性以在@Html.EditorFor的视图中显示以及如何将更改保存到数据库中(表UserProfile ).

I've added additional columns, such as FirstName etc. I needed and registration works correctly, but I don't know how to get this attributes to show in view for @Html.EditorFor and to save changes in database (table UserProfile).

非常感谢您的每一个提示.

Many thanks for every tip.

为版本创建的模型:

public class UserProfileEdit
    {
        [Required]
        [Display(Name = "First name")]
        public string FirstName { get; set; }

        [Required]
        [Display(Name = "Last name")]
        public string LastName { get; set; }

        [Required]
        [Display(Name = "Email")]
        [DataType(DataType.EmailAddress)]
        public string Email { get; set; }
    }

推荐答案

因此,您需要一个用于编辑用户个人信息的编辑页面.其他列已添加到UserProfile表中.

So you want an edit page for editing personal information of user. Additional columns are already added to UserProfile table.

首先,您需要一种用于编辑视图的操作方法.从数据库中获取用户并建立您的UserProfileEdit模型.

First of all you need an actionmethod for edit view. Fetch the user from database and build your UserProfileEdit model.

public ActionResult Edit()
{
            string username = User.Identity.Name;

            // Fetch the userprofile
            UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.Equals(username));

            // Construct the viewmodel
            UserProfileEdit model = new UserProfileEdit();
            model.FirstName = user.FirstName;
            model.LastName = user.LastName;
            model.Email = user.Email;

            return View(model);
}

当我们发布editform时,我们将发布UserProfileEdit模型.我们再次从数据库中获取UserProfile并更改发布的字段.

When we post the editform, we post UserProfileEdit model. We fetch the UserProfile from database again and change the posted fields.

    [HttpPost]
    public ActionResult Edit(UserProfileEdit userprofile)
    {
        if (ModelState.IsValid)
        {
            string username = User.Identity.Name;
            // Get the userprofile
            UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.Equals(username));

            // Update fields
            user.FirstName = userprofile.FirstName;
            user.LastName = userprofile.LastName;
            user.Email = userprofile.Email;

            db.Entry(user).State = EntityState.Modified;

            db.SaveChanges();

            return RedirectToAction("Index", "Home"); // or whatever
        }

        return View(userprofile);
    }

现在,它只是在您的视图中进行编码.我的看起来像这样:

Now it's just coding in your view. Mine looks like this:

@model UserProfileEdit

@using (Html.BeginForm("Edit", "Account"))
{
    @Html.EditorFor(model => model.FirstName)
     @Html.EditorFor(model => model.LastName)
     @Html.EditorFor(model => model.Email)

    <input type="submit" value="Save" />
}

如果您有大量的编辑模型字段,Automapper可能会有所帮助.此解决方案可编辑当前登录的用户,但是将用户名添加为操作参数相当简单.

Automapper might help if you have tons of fields for your edit model. This solution edits the current logged in user but adding username as action parameter is rather trivial.

这篇关于编辑用户个人资料详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 12:30