0

我想在注册后将一行表分配给任何用户。此表有一个名为 UserId 的列和一些其他列。我想将此表的 UserId 设置为新注册用户的 UserId 并将其他值设置为 0。我想在 Register 函数中执行此操作。但我不知道如何在 Account 控制器的 Register 功能中访问 UserID。我使用默认帐户控制器及其功能。Register函数的代码是:

[HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
4

2 回答 2

2

假设用户是你的模型,控制器应该是这样的

class Account:Controller{

[HttpPost]
ActionResult Register(User myModel){

var myUserId = myModel.UserId;

}
}

你的观点应该从这样的开始

@model User
于 2012-06-17T05:38:10.940 回答
1

如果您将创建时为用户生成的唯一标识符称为 UserId,您可以找到它,

[HttpPost]
public ActionResult Register(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        // Attempt to register the user
        MembershipCreateStatus createStatus;
        var user = Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

        if (createStatus == MembershipCreateStatus.Success)
        {
            var userId = (Guid) user.ProviderUserKey; //The unique identifier just created

            FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("", ErrorCodeToString(createStatus));
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}
于 2012-06-17T07:07:21.883 回答