Multiple Submit Buttons In ASP.NET MVC


On the Visual Studio, create new ASP.NET MVC Web Application project

Select Empty Template and Core Reference is MVC




In Controllers folder, create new controller named DemoController.cs as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LearnASPNETMVCWithRealApps.Controllers
{
    public class DemoController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Work1()
        {
            return View("Work1");
        }

        public ActionResult Work2()
        {
            return View("Work2");
        }

        public ActionResult Work3()
        {
            return View("Work3");
        }

    }
}

In Views/Demo folder, create new razor views as below:

Create new view named Index.cshtml as below:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm(FormMethod.Post))
    {
        <input type="submit" value="Work 1" formaction="@Url.Action("Work1", "Demo")"  />
        <input type="submit" value="Work 2" formaction="@Url.Action("Work2", "Demo")"  />
        <input type="submit" value="Work 3" formaction="@Url.Action("Work3", "Demo")"  />
    }
</body>
</html>

Create new view named Work1.cshtml as below:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Work 1</title>
</head>
<body>
    <div>
        Work 1
    </div>
</body>
</html>

Create new view named Work2.cshtml as below:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Work 2</title>
</head>
<body>
    <div>
        Work 2
    </div>
</body>
</html>

Create new view named Work3.cshtml as below:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Work 3</title>
</head>
<body>
    <div>
        Work 3
    </div>
</body>
</html>




Access Index action in Account controller with following url: http://localhost:9596/Demo/Index

Output

Click Work 1 button submit form to Work1 action in Demo controller

Output

Work 1

Click Work 2 button submit form to Work2 action in Demo controller

Output

Work 2

Click Work 3 button submit form to Work3 action in Demo controller

Output

Work 3

I recommend you refer to the books below to learn more about the knowledge in this article: