none
Jak pouzit individual usery? RRS feed

Odpovědi

  • Do té MVC tabulky by si neměl zasahovat. Vytvoř si pomocnou tabulku User a do ní pak přidej IDčko z té MVC tabulky. Potom si vytvoř view, model a controller nějak takto:

    Admin View:

    @model eshopmlg420.Models.AdminViewModel
    @{
        ViewBag.Title = "Admin";
    }

    <h2>Admin</h2>


    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            <h4>AdminViewModel</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.SelectRole, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(model => model.SelectRole,new SelectList(new[] {"Admin","User","Skladnik"}) ,new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.SelectRole, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(model => model.Users, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(model => model.Id,new SelectList(Model.Users,"ASPID","LastName"), new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.SelectRole, "", new { @class = "text-danger" })
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Create" class="btn btn-default" />
                </div>
            </div>
        </div>
    }

    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }

    Model:

       public class AdminViewModel
        {
            public string Id { get; set; }
            public string SelectRole { get; set; }

            public List<User> Users { get; set; }
        }

    AccountController:

     private Entities _db = new Entities();
            [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)
                    {
                        await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                        _db.User.Add(new User()
                        {
                            ASPID = user.Id,
                            FirstName = model.FirstName,
                            LastName = model.LastName,
                            Adress = model.Adress,
                            PhoneNumber = model.PhoneNumber,
                            Email = model.Email


                        });
                        _db.SaveChanges();
                        UserManager.AddToRole(user.Id, "User");
                        return RedirectToAction("Index", "Home");
                    }
                    AddErrors(result);
                }

                // If we got this far, something failed, redisplay form
                return View(model);
            }

    HomeController:

      private Entities _db = new Entities();

            [Authorize(Roles = "Admin")]

            public ActionResult Admin()
            {
                return View(new AdminViewModel() {Users = _db.User.ToList()});
            }

            private ApplicationDbContext ctx = new ApplicationDbContext();

            [Authorize(Roles = "Admin")]
            [HttpPost]
            public ActionResult Admin(AdminViewModel model)
            {
                if (ModelState.IsValid)
                {
                    var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(ctx));
                    manager.AddToRole(model.Id, model.SelectRole);
                    ctx.SaveChanges();
                }
                return RedirectToAction("Index");
            }

    RegisterView:

    @model eshopmlg420.Models.RegisterViewModel
    @{
        ViewBag.Title = "Register";
    }

    <h2>@ViewBag.Title.</h2>

    @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()
        <h4>Create a new account.</h4>
        <hr />
        @Html.ValidationSummary("", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.Label("Jméno", new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.Label("Přijmení", new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.LastName, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.Label("Adresa", new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.Adress, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.Label("Telefoní Číslo", new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.PhoneNumber, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" class="btn btn-default" value="Register" />
            </div>
        </div>
    }

    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }

    AccountViewModel:

     public class RegisterViewModel
        {
            [Required]
            [EmailAddress]
            [Display(Name = "Email")]
            public string Email { get; set; }

            [Required]
            [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
            [DataType(DataType.Password)]
            [Display(Name = "Password")]
            public string Password { get; set; }

            [DataType(DataType.Password)]
            [Display(Name = "Confirm password")]
            [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
            public string ConfirmPassword { get; set; }


            [Required]
            public string FirstName { get; set; }
            [Required]
            public string LastName { get; set; }
            [Required]
            public string Adress { get; set; }
            [Required]
            public string PhoneNumber { get; set; }
        }

        

    úterý 8. května 2018 16:52