Click here to Skip to main content
14,486,686 members
Rate this:
Please Sign up or sign in to 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

//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:

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







//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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
[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
@model List<string>
foreach(var userName in model)
{
    <a href="@Url.Action(" edit=", new { username = userName })">@userName</a>
}
</string>
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100