After Login, how to pass value to the view

Asked

Viewed 59 times

0

After logging in, the user is directed to the page Home and on this page, I would like to show a Label with the client code to which the user belongs.

So I went to the table AspNetUsers added a column called Cod_Cliente, then on AccountController tried:

 // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.Usuário, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                {
                    //recupera as informações do usuario que corresponda ao usuario e password
                    var user = await UserManager.FindAsync(model.Usuário, model.Password);


                    //redireciona o login para a pagina que o usuario estava.
                    //return RedirectToLocal(returnUrl);
                    //redireciona o login para o index do controller home
                    return RedirectToAction("Index", "Home");
                }                   
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Login Inválido!");
                return View(model);
        }
    }

However, the variable user receives an object with the user data, but the field I need Cod_Cliente is not among them.

So I tried to include the parameter in the model AccountViewModel

 public class LoginViewModel
{
    [Required]
    [Display(Name = "Usuário")]
    public string Usuário { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Permanecer Conectado?")]
    public bool RememberMe { get; set; }

    public bool Cod_Cliente { get; set; }
}

But to no avail.

Follows below the View Index of HomeController

    @using OneeWeb_v3.Models
@model LoginViewModel
@{
    ViewBag.Title = "Home";
}

<div class="jumbotron">
    <h2>Bem Vindo ao OneeWeb - @DateTime.Now.ToLongDateString()</h2>   
    <h3>@Html.DisplayNameFor(m => m.Cod_Cliente)</h3>
    <p class="lead">CNPJ: 054.105.671/xxxx-xx</p>
    @*<p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>*@
</div>

<div class="row">
    <div class="col-md-4">
        <h2>Getting started</h2>
        <p>
            ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
            enables a clean separation of concerns and gives you full control over markup
            for enjoyable, agile development.
        </p>
        <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301865">Learn more &raquo;</a></p>
    </div>
    <div class="col-md-4">
        <h2>Get more libraries</h2>
        <p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
        <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301866">Learn more &raquo;</a></p>
    </div>
    <div class="col-md-4">
        <h2>Web Hosting</h2>
        <p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
        <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301867">Learn more &raquo;</a></p>
    </div>
</div>
  • 1

    You need to create this field in the class that represents this table, if you haven’t modified anything in the default template, it should be the ApplicationUser (extending to IdentityUser). I suggest giving a read at that link to have a basis.

  • @Alisson friend thanks for the tip, I managed to do after the login that is returned the value, but I still could not show in the view. thanks.

1 answer

2


I would change the following;

Loginviewmodel

public int Cod_Cliente { get; set; }

Controller

ViewBag.CodigoUser = model.Cod_Cliente;

View

<h3>@Html.DisplayNameFor(ViewBag.CodigoUser)</h3>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.