Monday, April 19, 2021

MVC Methods to Transfer Data from Controller to View

ViewData is getter and setter property of ViewDataDictionary class. It is used to pass data from Controller to a View. Typecasting can be required in case of ViewData.

Example1: Transfer data from Controller to View using ViewData

Step1. Controllers/HomeController

using System.Collections.Generic;
using System.Web.Mvc;
//ViewDataDictionary
namespace WebViewData.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            string strData = "Ajeet Kumar";
            ViewData["strKey"] = strData;
            int number = 9999;
            ViewData["intKey"] = number;
            List surnames = new List() { "Gupta", "Sharma", "Sethi", "Ambani", "Kapoor", "Jain" };
            ViewData["objKey"] = surnames;

            return View();
        }
    }
}
Step2. Views/Index
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        ViewData is getter and setter property of ViewDataDictionary class.
        <br />
        It is used to pass data from Controller to a View.
        <br />
        Typecasting may be needed w.r.t. ViewData.
        <br />
        @ViewData["strKey"] <br />
        @ViewData["intKey"] <br />
       
        <ol>
        @foreach (var surname in (List<string>)ViewData["objKey"])
        {
            <li>@surname</li>
        }
        </ol>
        
    </div>
</body>
</html>
OUTPUT:
Ajeet Kumar
9999
  1. Gupta
  2. Sharma
  3. Sethi
  4. Ambani
  5. Kapoor
  6. Jain
Example2: Transfer data from Controller to View using ViewData, ViewBag and TempDate

Step1 Controllers/HomeController

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

namespace MvcController2View.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List numbers = new List() { 1,2,3,4,5};
            ViewData["Numbers"] = numbers;
            ViewBag.bag = numbers;
            TempData["nums"] = numbers;
            return View();
        }
    }
}
Step2 Views/Index

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        <h3>Method1: ViewData</h3>
        @foreach (var number in (List<int>)ViewData["Numbers"])
        {
            @number <br />
        }
    </div>
    <div>
        <h3>Method2: ViewBag</h3>
        @foreach (var number in ViewBag.bag)
        {
            @number <span>, </span>
        }
    </div>
    <div>
        <h3>Method2: ViewBag, SUM</h3>
        @{
            var sum = 0;
        }

        @foreach (var number in ViewBag.bag)
        {
            sum = sum + number;
        }
        @sum
    </div>

    <div>
        <h3>Method3: TempData</h3>
        @foreach (var number in (List<int>)TempData["nums"])
        {
            @number <span>, </span>
        }
    </div>
</body>
</html>

Output:






No comments:

Post a Comment

Hot Topics