【问题标题】:how to integrate AccountController(Registration) in CRUD with MVC如何将 CRUD 中的 AccountController(Registration) 与 MVC 集成
【发布时间】:2017-05-14 04:59:28
【问题描述】:

我目前正在处理一个 MVC 项目,该项目需要为顾问和客户提供 CRUD(2 个单独的表)。一开始我们只是为这些用户创建了 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; }
}

帐户控制器

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

【问题讨论】:

    标签: c# html asp.net-mvc model-view-controller


    【解决方案1】:

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

    1. 您的模型应该继承自 ApplicationUser 类,该类同为 Table per Type (TPT)
    2. 那么您应该在 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; }
            }
    

    AccountController 中的 Register 操作中

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

    更多细节,你可以看这个 Answer.

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

    【讨论】:

    • 感谢您的解决方案,我有一些疑问,希望您能提供帮助。
    • 感谢您的解决方案,我有一些疑问,希望您能提供帮助。在 JoinViewModel 中,我是否填充了我的客户模型和顾问模型中相同的字段?我将如何在视图中显示此注册过程?
    • 好的,我会帮助你修改演示,我会在这里评论它,但如果我理解你的意思,你问注册视图会是什么!如果答案有用,请将其标记为答案@ShriyaBramdeo
    • 非常感谢。如果有帮助,我可以向您展示我目前拥有的代码吗?
    • 是的,它会有所帮助!用它编辑问题,我会很高兴地帮助你
    猜你喜欢
    • 1970-01-01
    • 2013-05-21
    • 2011-02-01
    • 2013-07-12
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多