Tuesday, 21 November 2017

Pass (send) data from one Action method to another Action method in ASP.Net MVC

Passing data between different controller action methods in MVC

Description:


In this example we explain that how to pass data from one Action Method to another Action Method in Asp.Net MVC.or how to send data from one action method to another action method in MVC Razor. Or how to pass value from one action method to another action method in MVC controller. Or passing Data between different controller action methods in MVC.

Here we demonstrate that how to passing data from one controller action method to another method in MVC.
Controller:

public class EmployeeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Save()
        {
            TempData["Message"] = "You clicked on  Save Button!";
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Cancel()
        {
            TempData["Message"] = "You clicked on Cancel Button!";
            return RedirectToAction("Index");
        }
    }

View: 

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Pass (send) data from one Action method to another Action method in ASP.Net MVC</title>
</head>
<body>
    <table>
        <tr>
            <td>
                @using (Html.BeginForm("Save", "Employee", FormMethod.Post))
                {
                    <input type="submit" id="btnSave" value="Save"/>
                }
            </td>
            <td>
                @using (Html.BeginForm("Cancel", "Employee", 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>



This entry was posted in :

0 comments:

Post a Comment