WebSecurity.CreateUserAndAccount propertyValues(We

2019-08-17 00:00发布

我正在写一个MVC 4 C#.NET 4.5的网站

我想创建一个新的公司对象并注册链接到该公司的新用户。

我的帐户模式是:

    [Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
        public string PhoneNumber { get; set; }
        public bool MarketingEmailOptin { get; set; }
        public bool isDisabled { get; set; }
        public virtual Company CompanyICanEdit { get; set; } 
    }

如果我叫下面的它增加了用户不错,但都是空的CompanyICanEdit领域:

WebSecurity.CreateUserAndAccount(addCompanyViewModel.User.UserName,
                                 addCompanyViewModel.User.Password,
                                 propertyValues: new
                                 {
                                   FirstName = addCompanyViewModel.User.FirstName,
                                   LastName = addCompanyViewModel.User.LastName,
                                   EmailAddress = addCompanyViewModel.User.EmailAddress,
                                   PhoneNumber = addCompanyViewModel.User.PhoneNumber,
                                   MarketingEmailOptin = addCompanyViewModel.User.MarketingEmailOptin,
                                   isDisabled = false
                                 });

这是我所期望的,因为我不指定任何东西。

我已经尝试添加(MyCompany正在公司对象):

    WebSecurity.CreateUserAndAccount(addCompanyViewModel.User.UserName,
                                 addCompanyViewModel.User.Password,
                                 propertyValues: new
                                 {
                                   FirstName = addCompanyViewModel.User.FirstName,
                                   LastName = addCompanyViewModel.User.LastName,
                                   EmailAddress = addCompanyViewModel.User.EmailAddress,
                                   PhoneNumber = addCompanyViewModel.User.PhoneNumber,
                                   MarketingEmailOptin = addCompanyViewModel.User.MarketingEmailOptin,
                                   isDisabled = false,
                                   CompanyICanEdit = mycompany
                                 });

但我得到一个错误说不能匹配的类型。

我如何去使CompanyICanEdit包含myCompany中的CompanyId值注册用户?

任何帮助将不胜感激。 谢谢

Answer 1:

从来没有制定出如何做到这一点的1路走,传开通过它,如果任何人有同样的问题到底以下。

//
// POST: /BusinessManager/ManageCompanies/Add
[HttpPost]
public ActionResult Add(AddCompanyViewModel addCompanyViewModel)
{
    if (ModelState.IsValid)
    {
        // Create company and attempt to register the user
        try
        {
            WebSecurity.CreateUserAndAccount(addCompanyViewModel.User.UserName,
                                                addCompanyViewModel.User.Password,
                                                propertyValues: new
                                                {
                                                    FirstName = addCompanyViewModel.User.FirstName,
                                                    LastName = addCompanyViewModel.User.LastName,
                                                    EmailAddress = addCompanyViewModel.User.EmailAddress,
                                                    PhoneNumber = addCompanyViewModel.User.PhoneNumber,
                                                    MarketingEmailOptin = addCompanyViewModel.User.MarketingEmailOptin,
                                                    isDisabled = false
                                                });

            db.Companies.Add(addCompanyViewModel.Company);

            var newuser = db.UserProfiles.FirstOrDefault(u => u.UserName == addCompanyViewModel.User.UserName);
            if (newuser != null)
            {
                newuser.CompanyICanEdit = addCompanyViewModel.Company;
                db.Entry(newuser).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                ModelState.AddModelError("", "New user wasn't added");
            }                     
        }
        catch (MembershipCreateUserException e)
        {
            ModelState.AddModelError("", Mywebsite.Controllers.AccountController.ErrorCodeToString(e.StatusCode));
        }

    }

    return View(addCompanyViewModel);
}


文章来源: WebSecurity.CreateUserAndAccount propertyValues