I'm Currently working on a MVC project that requires CRUD for a consultant and a client (2 separate tables). In the beginning we just created CRUD for these users and now realize that we need to add a Registration and Login feature to this, is there any way of calling the built-in AccountController with the existing controller that we have generated?
Or is there a way of calling two or more models in a single controller or view?
Client Model
public class Client:ApplicationUser
{
[Key]
[Required]
public int Id { get; set; }
[Required]
[Display(Name = "Client Name")]
public string ClientName { get; set; }
[Required]
[Display(Name = "Client Address")]
public string ClientAddress { get; set; }
[Required]
[Display(Name = "Contact Number")]
public long ContactNumber { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[Display(Name = "Project Leader")]
public string ProjectLeader { get; set; }
public virtual ManageTravel ManageTravel { get; set; }
[Display(Name = "Rate")]
public double Rate { get; set; }
[Display(Name = "Distance")]
public string Distance { get; set; }
[Display(Name = "Rate")]
[Required]
public string TravelCode { get; set; }
Consultant Model
public class Consultant:ApplicationUser
{
[Key]
public int ConsultantNum { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[Display(Name = "Contact Number")]
public int ContactNumber { get; set; }
[Required]
[Display(Name = "Consultant Address")]
public string ConsultantAddress { get; set; }
[Required]
[Display(Name = "Email")]
[EmailAddress]
public string Email { get; set; }
[Required]
[Display(Name = "Consultant Type")]
public string ConsultantType { get; set; }
[Required]
[Display(Name = "Commission Code")]
public string ComissionCode { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[Display(Name = "Role Type")]
public string RoleType { get; set; }
}
AccountController
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// This code has been added to the action for email confirmation
var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action(
"ConfirmEmail",
"Account",
new { userId = user.Id, code = code },
protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(
user.Id,
"Confirm your account",
"Please confirm your account by clicking this link: <a href=\""
+ callbackUrl + "\">link</a>");
ViewBag.Link = callbackUrl;
return View("DisplayEmail"); // DisplayEmail View has been created
}
AddErrors(result);
//end of email confirmation code
}
// If we got this far, something failed, redisplay form
return View(model);
}