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

Asked

Viewed 666 times

3

I have the following problem:

On a given HTML page I have two buttons that call different POST methods in 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 by pressing the buttons I am NOT being redirected to the Userinfo1 and Userinfo2 methods (I notice this when I try to execute the operation by inserting breakpoints in these methods). Consequently I get a 404 error on the HTML page.

How can I access each of these methods? It is necessary to create some type of specific route?

  • Put all Controller Code

  • Controller code updated @Virgilio Novic.

  • 1

    Possible duplicate of How to put two action with HTTPPOST

  • You do not have the Default route?

  • Bardetta. I don’t use a default route. If this is the problem, how should I set it?

3 answers

2


Complementing...

You’re making a mistake because of the lack of route Default, because its configuration routes it always enters the controller and action Userinfo.

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

Also worth remembering that the route Default should always come last, as it will always enter the first route that "hit"

2

Your problem is that you don’t have Httpget action for your view to load.

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

For each Userinfo1 and Userinfo2 Action you need to have a View with the same name so mvc can find it when the action is called.

Look how it turned out controller and Views

  • I tried here but it didn’t work. I understand that this would be the problem if the execution was accessing any of the posts, however it would not even get inside the POST methods to return the view.

0

I managed to solve. The problem is that the default route was missing, which should be placed below 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 }
        );

Browser other questions tagged

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