0

我正在使用 MVC 4,并且正在尝试向名为 AdditionalById 的 UserProfile 添加一个新字段(用于管理事务)。

这是我所拥有的:

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new
                {
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    AddedDate = DateTime.UtcNow
                    AddedByID = // to sure how to get the id because the account has not been created yet.
                });

创建帐户后我应该只更新 AddById 吗?

4

1 回答 1

0

因为支持团队可以为某人创建帐户

如果是这种情况,请确保AddedByIDNullable,然后Support Team在创建User.

然后,让你Register Action看起来像这样;

    //
    // POST: /Account/Register

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            using (EfDb db = new EfDb())
            {                                     
                try
                {
                    var userProfile = db.UserProfiles.Local.SingleOrDefault(u => u.UserName == User.Identity.Name)
                                ?? db.UserProfiles.SingleOrDefault(u => u.UserName == User.Identity.Name);

                        WebSecurity.CreateUserAndAccount(
                            model.UserName,
                            model.Password,
                                new
                                    {
                                        FirstName = model.FirstName,
                                        LastName = model.LastName,
                                        AddedDate = DateTime.UtcNow,
                                        AddedByID = userProfile.UserId
                                    }
                            );

                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }
于 2013-05-19T20:21:44.937 回答