What are the different ways to pass/transfer content/data - MVC

Apr 23, 2019 BY xtreme

.

A. From View To Controller
	1. Transfer directly to the second View/Action
		<a href='@Url.Action("ActionTo", new ModelData { Name = "ItBlow" })'>click me</a>
		public ActionResult ActionTo(ModelData m){}

	2. POST data to a [HttpPost] Index action
		@using (Html.BeginForm()) { 		
			<input type="text" id="Name" name="Name"  value="ItBlow"/>
			<input type="submit" value="Click me" />
		}
		public ActionResult ActionTo(ModelData m){}
	
	
	
	
B. From Controller to View
	1. 	Using ViewData
		public ActionResult Index(){
            Record r = new Record
            {
                Id = 1,
                Name = "ItBlow",
            }; 
            ViewData["ModelData"] = r; 
            return View();
        }
		@{
			var record = (Record)ViewData["ModelData"];
			<h3>Name: @record.Name </h3>
		}
		
		
		OR
		public ActionResult Index(){
            var Record r = new List<string>
            {
                "Name1",
				"Name2",
				"Name3",
            }; 
            ViewData["ModelData"] = r; 
            return View();
        }
		@{
			@foreach (var r in (List<string>)ViewData["Students"])  
			//OR  
			@foreach (var r in (IEnumerable<string>)ViewData["Students"])
		}
		
	2.  Using TempData
		TempData object is used to pass data from one request to the next only
		public ActionResult TestTempData()
        {
            TempData["data"] = "Limit to current request only! Test it by refreshing the page!"; 
            return View();
        }
		@{
			<h2>@TempData["data"]</h2>
		}		
		
	3.  Using ViewBag
		ViewBag=ViewData + Dynamic wrapper around the ViewData dictionary
		public ActionResult TestTempData()
        {
            ViewBag.Message = "Some Message"; 
            return View();
        }
		@{
			<h2>@ViewBag.Message</h2>
		}	
		
	4.  Using ViewModel		
		public ActionResult TestTempData()
        {
			DataModel data = _repository.GetById(id); 
            return View(data);
        }
		@model Product.Models.DataModel
		
		OR
		public ActionResult Index()
        {
            return View(_repository.Table.ToList());
        }
		@model IEnumerable<Product.Models.DataModel>
	
	
	

C. Between Controllers
	1. Using Model
		public ActionResult Index(ModelTransfer m){           	             
			return RedirectToAction("Transfer", new ModelTransfer() { Age = 42, Name = "ItBlow" });            
		}
	2. Using TempData
		TempData object is used to pass data from one request to the next only. Unless we retrieve the data stored in the TempData, it remains in the memory of the application so next time if Index method is called it throws following exception.
		public ActionResult Index(ModelTransfer m){           	             
			TempData["MyModel"]=new ModelTransfer(){ Age = 42, Name = "ItBlow"};
			OR
			TempData.Add("MyTempData", "This data is coming from calling method.");

			return RedirectToAction("Transfer");            
		}
		public ActionResult Transfer()
        {
            return Content(TempData["MyTempData"].ToString());
        }

	3. By Session
	
	4. By Cache
	
	5. By Application

	6. By Cookies
		public ActionResult Index(ModelTransfer m){           	             
		   HttpCookie cook = new HttpCookie("Transfer");
		   
		   cook.Expires = DateTime.Now.AddSeconds(1);
		   cook.Value = "from transfer cookies";
		   Response.Cookies.Add(cook);
		   return RedirectToAction("Transfer");   
        }         
		
Related