How to receive information in a View?

Asked

Viewed 114 times

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 check if the user exists, if it exists send his name to view, if it does not exist, send nao.

I want to know how to show this information on view.

My view:

@{
    ViewBag.Title = "Login";
}

<h2>Olá Fulano</h2>
  • How’s your view?

  • It’s blank, I don’t know how to raise it

  • Actually, it’s just Hello John Doe (where so-and-so would be the username)

2 answers

5


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

inserir a descrição da imagem aqui

ViewBag is a variable that loads information to the view, and Message is a key where you will put a value, IE, can create other names as in this example here:

inserir a descrição da imagem aqui

There is also a variable called ViewData which has the same purpose, this is typed as a dictionary containing objects, in the example below we can see in more detail:

inserir a descrição da imagem aqui

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

  • 3

    Sria good to quote from where you got these omagens: http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-to-pass-data-from-controller-to-view-in-mvc/. Actually down there is written " 2017 C# Corner. All Contents are copyright of their Authors." So you can’t even pick them up. You can give an answer where from source you can copy it?

  • I’m sorry but I don’t have time, not for the next two months. so you can do whatever you want with that answer, you can even delete it if you want. My duty as a member of the community to pass information has been done.

5

For your case probably a simple viewBag should fit:

[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 do it another way too:

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

I put in the Github for future reference.

There are other ways.

Browser other questions tagged

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