C# 如何将CRUD中的AccountController(注册)与MVC集成

C# 如何将CRUD中的AccountController(注册)与MVC集成,c#,html,asp.net-mvc,model-view-controller,C#,Html,Asp.net Mvc,Model View Controller,我目前正在从事一个MVC项目,该项目要求顾问和客户使用CRUD(两个单独的表)。开始时,我们刚刚为这些用户创建了CRUD,现在意识到我们需要在其中添加注册和登录功能,有没有办法用我们生成的现有控制器调用内置AccountController 或者有没有一种方法可以在单个控制器或视图中调用两个或多个模型 客户机型号 public class Client:ApplicationUser { [Key] [Required] public int Id { get; set;

我目前正在从事一个MVC项目,该项目要求顾问和客户使用CRUD(两个单独的表)。开始时,我们刚刚为这些用户创建了CRUD,现在意识到我们需要在其中添加注册和登录功能,有没有办法用我们生成的现有控制器调用内置AccountController

或者有没有一种方法可以在单个控制器或视图中调用两个或多个模型

客户机型号

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; }
 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; }
}
顾问模式

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; }
 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);
    }
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务);
Link=callbackUrl;
返回视图(“DisplayEmail”);//已创建DisplayEmail视图
}
加法器(结果);
//电子邮件确认代码结束
}
//如果我们走到这一步,有些东西失败了,重新显示形式
返回视图(模型);
}

是的,您可以为现有机型添加这些功能

  • 您的模型应该继承自
    ApplicationUser
    类,该类的排序为
  • 然后您应该在AccountController中自定义身份验证(登录、注册)操作
  • 下面是一个完整的例子:

    模特顾问:

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

     [Table("Client")]
            public class Client: ApplicationUser
            {
                public string Name { get; set; }
                public string Address { get; set; }
                public string Password{ get; set; }
            }
    
     var user = new Consultant { .... };   
     var result = await UserManager.CreateAsync(user,model.Password);
    
    注册表中AccountController中执行操作

     [Table("Client")]
            public class Client: ApplicationUser
            {
                public string Name { get; set; }
                public string Address { get; set; }
                public string Password{ get; set; }
            }
    
     var user = new Consultant { .... };   
     var result = await UserManager.CreateAsync(user,model.Password);
    
    有关更多详细信息,请参见此


    我在这里为您创建了一个简单的演示

    谢谢您的解决方案,我有一些疑问,希望您能提供帮助。谢谢您的解决方案,我有一些疑问,希望您能提供帮助。在JoinViewModel中,我是否填充与我的clietn和consultant模型中相同的字段?我将如何在视图中显示此注册过程?好的,我会的帮助您修改演示,我将在这里对其进行评论,但如果我理解您的观点,您将询问注册视图的内容!如果答案有用,请将其标记为答案@shryabramdeo非常感谢。如果有帮助,我可以向您展示我目前拥有的代码?是的,会有帮助!用它编辑问题,我将我很乐意帮助你