In this article I will explain with an example, how to pass (send) data from one Action method to another Action method in ASP.Net MVC Razor.
This article will illustrate how to pass (send) data from one Action method to another Action method using TempData object in ASP.Net MVC Razor.
 
 
Controller
The Controller consists of three Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
 
Action method for handling POST operation – Form 1
This Action method gets called when the Form 1 is submitted due to the click of the Save button. It sets a string message in TempData object indicating that the Save button was clicked and then it redirects to the Index view.
 
Action method for handling POST operation – Form 2
This Action method gets called when the Form 2 is submitted due to the click of the Cancel button. It sets a string message in TempData object indicating that the Cancel button was clicked and then it redirects to the Index view.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult Save()
    {
        TempData["Message"] = "You clicked Save!";
        return RedirectToAction("Index");
    }
 
    [HttpPost]
    public ActionResult Cancel()
    {
        TempData["Message"] = "You clicked Cancel!";
        return RedirectToAction("Index");
    }
}
 
 
View
The following View markup consists of two Forms created using Html.BeginForm Helper methods and each Form consists of an HTML Submit Button.
At the end, there’s a JavaScript method that displays the TempData object message using JavaScript Alert Message Box.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <table>
        <tr>
            <td>
                @using (Html.BeginForm("Save", "Home", FormMethod.Post))
                {
                    <input type="submit" id="btnSave" value="Save"/>
                }
            </td>
            <td>
                @using (Html.BeginForm("Cancel", "Home", FormMethod.Post))
                {
                    <input type="submit" id="btnCancel" value="Cancel"/>
                }
            </td>
        </tr>
    </table>
  
    @if (TempData["Message"] != null)
    {
       <script type="text/javascript">
            window.onload = function () {
                alert('@TempData["Message"]');
            };
        </script>
    }
</body>
</html>
 
 
Screenshot
Pass (send) data from one Action method to another Action method in ASP.Net MVC
 
 
Downloads