I have the need to make a POST request from the controller before loading the view, I want to do this due to a certain condition that is required of me.
The condition is:
If I have more than one product in the contract, show a "Menu" with the contracted products, otherwise, if I only have 1, show the view of that product and not the "Menu".
The Menu controller code is:
public ActionResult MenuApps(){
if (WSKernel.Suscriptions_WebDevices_Get(productosDatos, out productosRespuesta)) {
model.Productos = productosRespuesta.ListSuscriptions_WebDevices;
//si solo se cuenta con un solo producto, hacer redireccion directa al unico producto
if (model.Productos != null && model.Productos.Length == 1){
switch (model.Productos[0].AdmixProduct){
case "Producto1":
return Redirect("http://localhost/Producto1");
case "Producto2":
return Redirect("http://localhost/Producto2");
}
}
}else{//sin productos}
}
But the do return Redirect("http://localhost/Producto2");
makes a request HTTP
because GET
I want to send information and I want the user not to see it just because I need him to always log in correctly.
On the other hand, where I want to redirect is another project that is already finished and this one is configured to receive information by POST.
How can I make the POST request and redirect to the page from the controller?
After several attempts and a lot of searching I found this post where he explains the attempts he made, but apparently my second idea was on the right track, only on the page he does it with a
Helper
.The idea of
Helper
is simple:for each key value you want to post, we create a hidden field, create the form, then add the necessary script to do the automatic submission by calling it
vPostForm.submit()
from the JavaScript code .Therefore the code would be as follows:
Now to consume this private method you need the following:
ok now in my controller i would use something like this:
Now yes, the condition that had to be respected is fulfilled:
Samer Abu Rabie code and credit
If you want to do something simpler in MVC there is a Nuget package like Fluentx.Mvc (Recommended) , in which you no longer need to create the helper in a file, you only need to import the package with
using Fluentx.Mvc;
To use it is the same way.