Two actions for the same View

Asked

Viewed 61 times

1

I have these two action:

public ActionResult Lista(int PessoaId, string Nome, string Twitter) 
{
    Pessoa p = new Pessoa();
    p.PessoaId = PessoaId;
    p.Nome = Nome;
    p.Twitter = Twitter;

    return View(p);

}

public ActionResult Lista2(FormCollection form)
{
    Pessoa p = new Pessoa();
    p.PessoaId = Convert.ToInt32(form["PessoaId"]);
    p.Nome = form["Nome"]; ;
    p.Twitter = form["Twitter"]; ;

    return View(p);

}

I have this View:

@model Hello.Models.Pessoa
@{
    ViewBag.Title = "Lista";
}

<h2>Lista</h2>

<div>
    <b>PessoaId</b>
</div>
<div>
    @Model.PessoaId
</div>

<div>
    <b>Nome</b>
</div>
<div>
    @Model.Nome
</div>

<div>
    <b>Twitter</b>
</div>
<div>
    @Model.Twitter
</div>

Can’t I add two actions to the same view? I tried to add the same View I added in the first action in the second action, but it was not allowed.

  • is aspnet .....?

  • Yes, it is ASPNET!!

1 answer

3


I can’t add two action to a same view?

Yes CAN, just specify the name of your View in both Action, example:

public ActionResult Lista(int PessoaId, string Nome, string Twitter) 
{
    Pessoa p = new Pessoa();
    p.PessoaId = PessoaId;
    p.Nome = Nome;
    p.Twitter = Twitter;

    return View("NomeDaView", p); 

}

public ActionResult Lista2(FormCollection form)
{
    Pessoa p = new Pessoa();
    p.PessoaId = Convert.ToInt32(form["PessoaId"]);
    p.Nome = form["Nome"]; ;
    p.Twitter = form["Twitter"]; ;

    return View("NomeDaView", p);

}

Where is Nomedaview put the file name .cshtml, example Lista.cshtml, then, return View("Lista", p);.

References

Browser other questions tagged

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