ASP.NET - How to use multiple post methods in the same controller?

3

I have the following problem:

On a given HTML page I have two buttons that call different POST methods on the same controller.

@using (Html.BeginForm("UserInfo1", "UserInfo", FormMethod.Post))
{
    <input type="submit" value="Ir para Action" 
     name="botao1" id="botao1" />
}

@using (Html.BeginForm("UserInfo2", "UserInfo", FormMethod.Post))
{
    <input type="submit" value="Ir para Action" 
     name="botao2" id="botao2" />
}

The code on my controller is as follows:

using Project1.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace Project1.Controllers
{
    public class UserInfoController : Controller
    {
        [HttpGet]
        public ActionResult UserInfo()
        {
           return View();
        }

        [HttpPost]
        public ActionResult UserInfo1(string g)
        {
            @Viewbag.User1 = g;
            return View();
        }

        [HttpPost]
        public ActionResult UserInfo2(string g)
        {
            @Viewbag.User2 = g;
            return View();
        }
    }
}

The route configured for this controller is:

routes.MapRoute(
            name: "UserInfo",
            url: "profile/",
            defaults: new { controller = "UserInfo", action = "UserInfo", id = UrlParameter.Optional }
        );
The big question is that pressing the buttons is NOT being redirected to the UserInfo1 and UserInfo2 methods (I realize this when I try to perform the operation by inserting breakpoints into those methods). Consequently I get a 404 error in the HTML page.

How can I get access to each of these methods? do I need to create some kind of specific route?

    
asked by anonymous 06.06.2018 / 04:39

3 answers

2

Complementing ...

It is giving error because of the Default route, since its route configuration always goes into the controller and action UserInfo .

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

It was also worth remembering that the route Default should always come last, since it will always enter the first route that "hits"

    
06.06.2018 / 22:27
2

Your problem is that you have no Action of type HttpGet for your view to be loaded.

[HttpGet]
public ActionResult UserInfo1()
{
    return View();
}
[HttpGet]
public ActionResult UserInfo2()
{
    return View();
}

For each Action UserInfo1 and UserInfo2 you need to have a View with the same name for mvc to find when Action is called.

See how it went controller and Views

    
06.06.2018 / 13:27
0

I managed to resolve. The problem is that the default route was missing, which should be placed below the custom routes:

routes.MapRoute(
        name: "UserInfo",
        url: "profile/",
        defaults: new { controller = "UserInfo", action = "UserInfo", id = UrlParameter.Optional }
    );

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    
06.06.2018 / 22:18