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
– novic
Controller code updated @Virgilio Novic.
– Diego Silva
Possible duplicate of How to put two action with HTTPPOST
– Amadeu Antunes
You do not have the Default route?
– Barbetta
Bardetta. I don’t use a default route. If this is the problem, how should I set it?
– Diego Silva