8

我正在写一个 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 值?

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

4

1 回答 1

20

从来没有弄清楚如何在 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);
}
于 2013-02-20T18:45:45.303 回答