Parameters between pages (ASP.NET Core / C#)

Asked

Viewed 324 times

2

Hello, I have a question in which I already researched a lot and did not get any concrete answer. I am beginner in C# and ASP.NET, forgive me for ignorance.

I have a project in ASP.NET Core MVC.

On it I have Page A and Page B.

On Page A I have an "Add Page B Item" button, on which when clicked, I need you to go to Page B by pressing the ID of Page A.

I wonder if there is any way to do this without using JS.

  • the request for page B will be GET or POST ? if it is GET, pass the parameter in the URL, Controller/Action/{Parametros} if it’s by post, you can use a form...etc...

  • It will be a POST, it happens that this button is already inside a form, can I have one form inside the other? I thought of suddenly do using Redirecttoaction, but it is GET, and as it is an ID, it is easy for the user to manipulate in the URL

  • I don’t know if you can have one form inside the other, but I don’t know what would be the use of that, since it will leave the page...

  • It would basically pass the parameter to the controller and the controller redirect to the B page passing the ID of the A by a Viewbag if you want I can mount an ex as a response to an ex

1 answer

2

I managed to solve by placing the following function on the onclick of the button:

onclick="location.href='@Url.Action("Action", "Controller")/'+$('#IdPai').val()"

Therefore, in the Controller Action I receive an id as parameter and within the method I create a new object with the id in question. Here’s an example of how it looks in the controller

public IActionResult Create (int id = 0) {
    var Objeto = new ObjetoViewModel();
    Objeto.IdExterno = id;
    return View(objeto);
}

Browser other questions tagged

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