Wednesday, June 2, 2021

MVC View To View Data Transfer - How to send data from one view to another view


HomeController.cs
using System.Web.Mvc;

namespace WebView2View.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View("View1");
        }
        public ActionResult View2()
        {
            return View();
        }
    }
}
View1.cshtml
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>View1</title>
</head>
<body>
    <div> 
        <form action="~/Home/View2" method="post">
            <input type="text" name="text1" value="Ajeet" />
            <br />
            <input type="text" name="text2" value="Kumar" />
            <br />
            <input type="submit" name="submit" value="Go to View2" />
        </form>
    </div>
</body>
</html>
View2.cshtml
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>View2</title>
</head>
<body>
    <div>
        <h4>You are at View2</h4>
        Data of form will appear in the address bar of browser.
        
    </div>
</body>
</html>
OUTPUT

On clicking Go to View2 button




No comments:

Post a Comment

Hot Topics