2013-02-16 10 views
7

sto scrivendo un MVC 4 C# .NET 4.5 sitopropertyValues ​​WebSecurity.CreateUserAndAccount

voglio creare un nuovo oggetto sociale e registrare un nuovo utente che è collegato a tale società.

Il mio modello account è:

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

Se chiamo il seguente si aggiunge la multa utente, ma ha nulla per il campo 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 
           }); 

che mi sarei aspettato come io non sono assegnando è tutto

Ho provato ad aggiungere (mycompany è un oggetto società):

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

ma ottengo un errore che dice che non può corrispondere al tipo.

Come faccio a registrare l'utente in modo che CompanyICanEdit contenga il valore CompanyId della mia azienda?

Qualsiasi aiuto sarà apprezzato. grazie

risposta

19

Non ho mai provato come farlo in 1 go, l'ho arrotondato alla fine se qualcuno ha lo stesso problema.

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

Se si aggiunge int CompanyICanEditId {get; impostato; } e [ForeignKey ("CompanyICanEditId")] per la proprietà CompanyICanEdit, quindi è possibile aggiungere CompanyICanEditId = company.Id alle proprietà dell'utente creato – blazkovicz