MVC error when passing model data between views

0

I am developing an app to start, you must ask the user some data to auenticarse (username and password) but on different screens, since it is a totem (without keyboard or mouse) and on each screen displays a virtual keyboard , which is alphabetic for the user, and numeric for the password.

Well, in that context, I have a MVC C # controller, which has an index, which is the presentation screen, and then 2 more views, one for the user, and another for the password. In summary, this works well, since in each screen I rescue the data (user and password) and I pass them to a defined model in the following way:

    public class Usuario
{
    public Usuario()
    { }

    [Display(Name = "Nombre Usuario")]
    public string UserName { get; set; }

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

The problem is that when going to the authentication process (defined in a business layer), the variable UserName is lost after I rescue the password.

here is the controller that I have implemented:

public class HomeController : Controller
{

    private Usuario usuario = new Usuario();

    public ActionResult Index() // pantalla de inicio
    {
        return View();
    }

    public ActionResult IngresaUsuario() 
    {
        return View();
    }



    public ActionResult IngresaPassword()
    {
        usuario.UserName = Request.Form["_usuario"];

        return View();
    }


    public ActionResult EvaluaAutenticacion()
    {

        usuario.Password = Request.Form["_password"];


        return RedirectToAction("SeleccionaPacientes");
    }

in the actionReslt EvaluaAutenticacion, I only have the password as data, since the user was null

The question is: how should I declare the user variable? , because even if you declare it as public, you still lose the value of the user.

Greetings

    
asked by Luis Gabriel Fabres 24.03.2017 в 15:23
source

1 answer

0

The problem is that the data only live inside the application , I mean that even if you declare class variables, your information no is maintained when you do different actions.

Try the following: modify IngresaPassword and send your object to the view:

public ActionResult IngresaPassword()
{
    usuario.UserName = Request.Form["_usuario"];

    return View(usuario);
}

and in the view add as the first line (or after the @using you have):

@model TuNamespace.Models.Usuario

and modify your form by adding a

<input type="hidden" value="@Model.UserName" name="_usuario">

This way you can get the data in EvaluaAutenticacion :

[HttpPost]
public ActionResult EvaluaAutenticacion()
{
    usuario.UserName = Request.Form["_usuario"];
    usuario.Password = Request.Form["_password"];
    ...
    return RedirectToAction("SeleccionaPacientes");
}

Do you see that HttpPost that is just above the action? Indicates the call to action mode, post . It is recommended that you call with post or put the actions that send information to the server; to do so, just add method="post" to your% tag form in the view:

<form action="/Home/EvaluaAutenticacion" method="post">

And as a final comment, do not do authentication in Home , create a specific driver for different groups of actions, in this case the control of users.

    
answered by 24.03.2017 / 15:45
source