0

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

1 Answers1

0

Yes you can add those features for your existing models

  1. Your models should inherite from ApplicationUser class , which cosider as Table per Type (TPT)
  2. then you should customize the authentication ( login , Register ) actions in AccountController

Here is acomplete example :

Model Consultant :

 [Table("Consultant")]
    public class Consultant: ApplicationUser
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public string Password{ get; set; }
    }

Model Client :

 [Table("Client")]
        public class Client: ApplicationUser
        {
            public string Name { get; set; }
            public string Address { get; set; }
            public string Password{ get; set; }
        }

In the Register Action In AccountController

 var user = new Consultant { .... };   
 var result = await UserManager.CreateAsync(user,model.Password);

For More Details , You can see this Answer.

i Created A simple demo for you here Simple Demo

Community
  • 1
  • 1
AlameerAshraf
  • 872
  • 1
  • 13
  • 23
  • Thank you for the solution, i have a few queries, hoping you can help. – Shriya Bramdeo May 13 '17 at 15:26
  • Thank you for the solution, i have a few queries, hoping you can help. In the JoinViewModel, do i populate fields that are the same in both my clietn and consultant model? How would i display this register process in a view? – Shriya Bramdeo May 13 '17 at 15:33
  • Ok I will help you be modifying the Demo and i will comment it here, but if I understand your point, you asking about the register view what it would be! and please if the answer was useful mark it as an answer @ShriyaBramdeo – AlameerAshraf May 13 '17 at 15:51
  • Thank you soo much. i can show you the code I have currently if that would help? – Shriya Bramdeo May 13 '17 at 20:15
  • Yes it would help ! Edit the question with it and i will help you happily – AlameerAshraf May 13 '17 at 20:33
  • I have added my code above. thank you so much for your help, really appreciate it. – Shriya Bramdeo May 13 '17 at 21:00
  • The [Demo](https://gist.github.com/AlameerAshraf/c18596b0e34bd49ceef0defbd7b3d535) Updated, See it and i will be there! – AlameerAshraf May 13 '17 at 22:54