mvc recover data from view and refresh

0

I have only one view in my project, of which the user has to enter a client name to look for it in a mailbox.

<form action="registrarCliente" method="post">

<input id="textCliente" name="textCliente" /> <input id="btnAdd" 
type="submit" value="Añadir" />
</form>
<form action="eliminarCliente" method="post">

<input id="textCliente" name="textCliente" /> <input id="btnDel" 
type="submit" value="Eliminar" />
</form>

This way I recover the data of the view, but when passing it to the controller, it opens another url address with a view that does not exist.

    [HttpGet]
    public ActionResult registrarCliente(String nombre, String textCliente) 
    {

        CorreosServices correosServices = new CorreosServices();

        correosServices.añadirCliente(textCliente);

        return View("/home/index");

    }

    [HttpGet]
    public ActionResult eliminarCliente(String nombre, String textCliente)
    {

        CorreosServices correosServices = new CorreosServices();

        correosServices.eliminarCliente(textCliente);

        return View("/home/index");

    }

I would like to refresh the page of / home / index, instead of returning the url / registerCliente or / deleteCliente, which do not exist.

    
asked by Hector Lopez 07.02.2018 в 10:36
source

1 answer

1

To begin with you should correct the error you have in the attributes of the actions. In the specific actions, the attribute HttpGet (will respond to the calls made with the GET method), while in the form you specify that you use the POST method ( method="post" ) with which your code will never be executed.

Modify your code to set the [HttpPost] attribute in your actions.

To return the view corresponding to Home/Index you have several alternatives:

If the actions registrarCliente and eliminarCliente are in the same controller you just need to call the method View with the name of the view:

return View("Index");

If they are in another controller you could choose to move the view Index to the folder Shared and call it the same (only with the name) or specify the full path of the view:

return View("~/Views/Home/Index.cshtml");

Another option is to perform a redirection to the action Home/Index with which the code defined in this action will also be executed (which in the previous cases would not be executed):

return RedirectToAction("Index", "Home");
    
answered by 07.02.2018 / 11:14
source