WebSecurity.CreateUserAndAccount propertyValues

I am writing mvc 4 website C # .net 4.5

I want to create a new company object and register a new user who is associated with this company.

My account model:

[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; } } 

If I call the following, it adds the user a penalty, but is null for the CompanyICanEdit field:

 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 }); 

which I would expect, since I do not assign it.

I tried to add (mycompany is a company object):

  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 }); 

But I get an error that does not match the type.

How do I switch to user registration, so that CompanyICanEdit contains my CompanyId value?

Any help would be appreciated. thanks

+7
source share
1 answer

I never worked on how to do this for the 1st time, in the end I turned out to be the following: if someone has the same problem.

 // // 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); } 
+19
source

All Articles