I am a newbie when it comes to ASP.NET MVC so it would be great if anyone could provide guidance with the following scenario.
Loooking at a scenario whereby there will be two types of members that can register to a site, clients and suppliers.
Each type will be a member with additional information being stored in separate models/tables (i.e. Client model/table, Supplier model/table), along with default member credentials (username, email, password). Not sure of the best approach for this. Any recommendations welcome?
There will be a separate register page for each type, whereby upon successful registration they will be added to their respective role (i.e. Client, Supplier).
Here is the Client model that I'm playing with.
public class Client
{
// Will set this manually when creating a Client member
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ClientId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Currently I've created a ViewModel as follows to handle registration for a Client
public class RegisterClient
{
// Default ASP.NET MVC RegisterModel
public RegisterModel RegisterModel { get; set; }
public Client Client { get; set; }
}
Below are the Register methods in the ClientController
public ActionResult Register()
{
return View();
}
[HttpPost]
public ActionResult Register(RegisterClient model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.RegisterModel.UserName, model.RegisterModel.Password, model.RegisterModel.Email, null, null, true, null, out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
FormsAuthentication.SetAuthCookie(model.RegisterModel.UserName, false /* createPersistentCookie */);
Roles.AddUserToRole(model.RegisterModel.UserName, "Client");
// Get UserId for Client <-> User relationship
int id = int.Parse(Membership.GetUser(model.RegisterModel.UserName).ProviderUserKey.ToString());
model.Client.UserProfileId = id;
db.Clients.Add(model.Client);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", createStatus.ToString());
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Is this a suitable approach? I was thinking of having the Client model inheriting MembershipUser.
How can I then create a page listing of Clients along with their membership details (i.e. username and email)?