Pass parameter to another page

Asked

Viewed 668 times

1

Within people, I own the accounts receivable table, where I am calling the accounts receivable create page, and Edit. So far, it’s working perfectly. However, within receivables, I have one select, where I already wanted to come selected the person I was. And also has the return list.

I needed to know how I can do, for example, passing a parameter, whether it is being called accounts receivable or people, for, when returning, I do the treatment to know where to return.

Here is the create:

 <a asp-page="/ContaReceber/Create" class="btn btn-primary btn-sm">Criar nova Conta</a>

And here the Edit:

 <a asp-page="/ContaReceber/Edit" asp-route-id="@item.Id" class="btn btn-sm btn-success">Editar</a>

He redirects, but I need to treat, as I reported.

This is the person ID parameter I need to pass to the other page:

  <input type="hidden" asp-for="PessoaVM.Pessoas.Id" name="id" id="id" />

Edit: Can pass id to Contareceber/Create page this way:

  <a asp-page="/ContaReceber/Create" asp-route-id="@Request.Query["id"]" class="btn btn-primary btn-sm">Criar nova Conta</a>

But I don’t know how to handle it in here:

  public IActionResult OnGet()
    {
        ContasReceberVM = new ContasReceberViewModel
        {
            ContasReceber = new ContasReceber(),
            PessoaVM = _context.Pessoas.ToList(),
            PlanosServicos = _context.PlanosServicos.ToList()
        };
        return Page();
    }

    public IActionResult OnGet(int id)
    {
        ContasReceberVM = new ContasReceberViewModel
        {
            ContasReceber = new ContasReceber(),
            PessoaVM = _context.Pessoas.Where(m => m.Id == id).ToList(),
            PlanosServicos = _context.PlanosServicos.ToList()
        };
        return Page();
    }

It returns the following error:

Multiple handlers Matched. The following handlers Matched route data and had all constraints satisfied:

Microsoft.AspNetCore.Mvc.Iactionresult Onget(), Microsoft.AspNetCore.Mvc.Iactionresult Onget(Int32)

  • to pass this to another page, you could use a Querystring, right? Why don’t you do that?

  • But how can I pass through ? In the case here I call the page, /Contareceber/Create and I need to pass the idpessoa, which is a Hidden.

  • @marianac_costa You don’t need to use code snippet in code that won’t run itself. In these cases you can only format the code with 4 spaces.

  • I don’t know how to do on Asp.net-core, but the idea would be: ContaReceber/Create?PessoaId=@IDPessoaAqui

  • Yeah, the idea would be this, this I’m trying to, but I’m not getting.

  • @marianac_costa could show us what he has tried to do?

  • @Wallacemaxters edited the question. Remembering that I have the load of the create page without the parameter, and with the parameter.

  • It is already receiving the normal id, I am passing in onget(int id), but when the id comes empty, it does not load the select, I need to treat that if it is empty behind all, else it brings the id.

  • Is more or less that?

Show 5 more comments

1 answer

5


Instead of two actions has only one and allows the ID be it nullable and do the treatment inside her.

public IActionResult OnGet(int? id)
{
    if (id.HasValue)
    {
        ContasReceberVM = new ContasReceberViewModel
        {
            ContasReceber = new ContasReceber(),
            PessoaVM = _context.Pessoas.Where(m => m.Id == id).ToList(),
            PlanosServicos = _context.PlanosServicos.ToList()
        };
    }
    else
    {
        ContasReceberVM = new ContasReceberViewModel
        {
            ContasReceber = new ContasReceber(),
            PessoaVM = _context.Pessoas.ToList(),
            PlanosServicos = _context.PlanosServicos.ToList()
        };
    }
    string urlAnterior = Request.Headers["Referer"].ToString();

    if (urlAnterior.Contains("Pagina"))
        return RedirectToAction("");
    else
        return RedirectToAction("");
}

And in the view the button can stay like this:

<a asp-controller="ContaReceber" asp-action="Edit" asp-route-id="PessoaVM.Pessoas.Id" class="btn btn-sm btn-success">Editar</a>
  • I arrived in this reply @Barbetta .. A question, how can I do in case when saving, he know where to redirect? I can use the same id ?

  • you want to know from which page it came before?

  • This, so that at the time you save, or he clicks back, he returns to the correct page.

  • @marianac_costa updated the answer, with the code he picks up the previous page

  • If I do this way, it goes back to an updated normal create and not to that of people or accounts receivable.

  • you are giving postback on the page? I believe that is why, I will see here if I find an easy solution to this.

Show 1 more comment

Browser other questions tagged

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