After login, how to pass value to the view

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 that this user belongs to.

So I went to the AspNetUsers table and added a column called Cod_Cliente , so in AccountController I 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's data, but the field I need Cod_Cliente is not between them.

Then 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 with no success.

Below is 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>
    
asked by anonymous 11.07.2017 / 17:04

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>
    
11.07.2017 / 19:21