How to send data from one Controller to another Controller in MVC

As we know that TempData can be used for passing value from Controller to View and also from Controller to Controller. So In this article, we will learn how to pass (send) from one controller to another controller.

Frist controller (HomeController)

using MVCCODESOLUTIONS.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCCODESOLUTIONS.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            TempData["Message"] = "Hello Everyone!! Code Solutions page refer to improve your code skills. you can also learn many theoretical questions that will help you in interview time.";
            return new RedirectResult(@"~\Welcome\");
        }

       

    }
}

Second Controller (WelcomeController)

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

namespace MVCCODESOLUTIONS.Controllers
{
    public class WelcomeController : Controller

    {
        // GET: Welcome
        public ActionResult Index()
        {
            return View();
        }
    }
}


View (Index.cshtml)

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title> How to send data from one Controller to another Controller in MVC</title>
</head>
<body>
    @TempData["Message"];
</body>

</html>


OutPut:-

How to send data from one Controller to another Controller in MVC How to send data from one Controller to another Controller in MVC Reviewed by NEERAJ SRIVASTAVA on 12:46:00 AM Rating: 5

No comments:

Powered by Blogger.