How to receive information in a View?

0

I programmed my controller as follows:

[HttpPost]
public ActionResult Login(string pUsuario, string pSenha)
{
     usuario oUsuario = modelOff.usuarios.Where(p => p.usuario1 == pUsuario && p.senha == pSenha).SingleOrDefault();
     if (oUsuario == null)
         return View("nao");
     else
         return View(oUsuario.usuario1);
}

I want to know how to display this information in view .

My view :

@{
    ViewBag.Title = "Login";
}

<h2>Olá Fulano</h2>
    
asked by anonymous 28.07.2017 / 16:55

2 answers

4

Hello, There are several ways to pass data to View one of them is this:

ViewBagisavariablethatloadsinformationtoview,andMessageisakeywhereyouwillputavalue,thatis,youcancreateothernamesasinthisexamplehere:

ThereisalsoavariablecalledViewDatathathasthesamepurpose,thisistypedasadictionarycontainingobjects,intheexamplebelowwecanseeinmoredetail:

There are other ways to pass data, but I think the ones I mentioned solve your problem.

    
28.07.2017 / 17:27
5

For your case, a simple viewBag should probably serve:

[HttpPost]
public ActionResult Login(string pUsuario, string pSenha) {
     usuario oUsuario = modelOff.usuarios.Where(p => p.usuario1 == pUsuario && p.senha == pSenha).SingleOrDefault();
     ViewBag.usuario = oUsuario == null ? "nao" : oUsuario.usuario1;
     return View();
}

View:

//precisa colocar onde está o modelo aqui, mas na pergunta não tem a localização
@{
    ViewBag.Title = "Login";
}

<h2>Olá Fulano @ViewBag.usuario</h2>

You can also do it differently:

[HttpPost]
public ActionResult Login(string pUsuario, string pSenha) {
     usuario oUsuario = modelOff.usuarios.Where(p => p.usuario1 == pUsuario && p.senha == pSenha).SingleOrDefault();
     ViewBag.usuario = oUsuario == null ? "nao" : oUsuario.usuario1;
     return View((object)(oUsuario == null ? "nao" : oUsuario.usuario1));
}

View:

@model string
@{
    ViewBag.Title = "Login";
}

<h2>Olá Fulano @Model</h2>

There are other ways.

    
28.07.2017 / 17:28