Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi all
I Implemented a edit page for asp.net identity user table
I want to pass Id to action to find the user
but I should add Id to the end of the Url to give me the view
how can I do that?
or tel me if there is better way to take the view!
thanks

C#
//edit.cshtml
@using (Html.BeginForm()) {
    @Html.HiddenFor(x => x.Id)
    <div class="form-group">
        <label>Email</label>
        @Html.TextBoxFor(x => x.Email, new { @class = "form-control" })
    </div>


What I have tried:

C#
//UserController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AuthenticationUsers.Models;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;

namespace AuthenticationUsers.Controllers
{
    public class UserController : Controller
    {
        
        public ActionResult Index()
        {
            return View(UserManager.Users);
        }
        public async Task<ActionResult> Edit(string Id)
        {
            WorldUser user = await UserManager.FindByIdAsync(Id);
            if (user != null)
            {
                ViewBag.Id = Id;
                return View(user);
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
        [HttpPost]
        public async Task<ActionResult> Edit(string Id, string email, string password)
        {
            WorldUser user = await UserManager.FindByIdAsync(Id);
            if (user != null)
            {
                user.Email = email;
                IdentityResult validEmail
                = await UserManager.UserValidator.ValidateAsync(user);
                if (!validEmail.Succeeded)
                {
                    AddErrorsFromResult(validEmail);
                }
                IdentityResult validPass = null;
                if (password != string.Empty)
                {
                    validPass
                    = await UserManager.PasswordValidator.ValidateAsync(password);
                    if (validPass.Succeeded)
                    {
                        user.PasswordHash =
                        UserManager.PasswordHasher.HashPassword(password);
                    }
                    else
                    {
                        AddErrorsFromResult(validPass);
                    }
                }
                if ((validEmail.Succeeded && validPass == null) || (validEmail.Succeeded
                && password != string.Empty && validPass.Succeeded))
                {
                    IdentityResult result = await UserManager.UpdateAsync(user);
                    if (result.Succeeded)
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        AddErrorsFromResult(result);
                    }
                }

            }
            else
            {
                ModelState.AddModelError("", "User Not Found");
            }
            return View(user);
        }
        private void AddErrorsFromResult(IdentityResult result)
        {
            foreach (string error in result.Errors)
            {
                ModelState.AddModelError("", error);
            }
        }
        private AppUserManager UserManager
        {
            get
            {
                return HttpContext.GetOwinContext().GetUserManager<AppUserManager>();
            }
        }
    }
}







C#
//routeconfig

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace AuthenticationUsers
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "app", action = "Index", id = UrlParameter.Optional }
            );
            
        }
    }
}
Posted
Updated 6-Sep-16 0:44am
v4

1 solution

I hope you will be securing these Edit user pages with Authorize attribute and other security.

But to answer your question, create a FindUser page and have a set of controls which lets the editor select a user to edit.

Then when the user to be edited is identified (e.g. in a grid, or search result) send a view model from the FindUser page to the Edit action as a string
e.g.
C#
[Authorize(Roles = "Admin")] //  Example security check
[HttpGet]
public Action FindUser()
{
    UsersContext = new ApplicationDbContext();
    var userNames = UsersContext.Users.Select(x=>x.Name).ToList(); // This can be your view model.
    return View(userNames);
}


FindUser view
mvc
@model List<string>
foreach(var userName in model)
{
    <a href="@Url.Action(" edit=", new { username = userName })">@userName</a>
}
</string>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900