0% found this document useful (0 votes)
56 views

View Data View Bag Temp Data

The document provides examples of using action verbs, ViewBag, ViewData, and TempData in ASP.NET MVC. It demonstrates creating a Student model class, HomeController with GET and POST actions, and views to display and submit student data. The ViewBag example displays the total student count. The ViewData example loops through a student list. The TempData example demonstrates passing data between controller actions by storing it in TempData and retrieving on subsequent actions within the same request.

Uploaded by

Snehal Dhas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

View Data View Bag Temp Data

The document provides examples of using action verbs, ViewBag, ViewData, and TempData in ASP.NET MVC. It demonstrates creating a Student model class, HomeController with GET and POST actions, and views to display and submit student data. The ViewBag example displays the total student count. The ViewData example loops through a student list. The TempData example demonstrates passing data between controller actions by storing it in TempData and retrieving on subsequent actions within the same request.

Uploaded by

Snehal Dhas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Action Verb Example

1. Create Model Student

using System;
using System.Web;
namespace ActionGetPostDemo.Models
{
public class Student
{
public int RollNO { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string City { get; set; }
}
}

2. Create HomeController

using System;
using System.Web.Mvc;
using ActionGetPostDemo.Models;

namespace ActionGetPostDemo.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Student s)
{
ViewBag.RNO=s.RollNO;
ViewBag.Name=s.Name;
ViewBag.Email=s.Email;
ViewBag.Add=s.Address;
ViewBag.City=s.City;
return View(s);
}

}
}

3. Create Strongly type View


@model ActionGetPostDemo.Models.Student

@{
ViewBag.Title = "Index";
}

<h2>Student Registration</h2>
@if (@ViewBag.RNO != null)
{
<h2>Roll No : @ViewBag.RNO</h2>
<h2>Name : @ViewBag.Name</h2>
<h2>Email : @ViewBag.Email</h2>
<h2>Address : @ViewBag.Add</h2>
<h2>City : @ViewBag.City </h2>
}
@using (@Html.BeginForm("Index", "Home"))
{
<table>
<tr>
<td>Roll No</td>
<td>@Html.TextBoxFor(Model => Model.RollNO)</td>
</tr>
<tr>
<td>Name</td>
<td>@Html.TextBoxFor(Model => Model.Name)</td>
</tr>
<tr>
<td>Email</td>
<td>@Html.TextBoxFor(Model => Model.Email)</td>
</tr>
<tr>
<td>Address</td>
<td>@Html.TextBoxFor(Model => Model.Address)</td>
</tr>
<tr>
<td>City</td>
<td>@Html.TextBoxFor(Model => Model.City)</td>
</tr>

<tr>
<td colspan="2">
<center><input type="submit" value="Register" /></center>
</td>
</tr>
</table>
}

ViewBag Example
1. Create Class in model folder

using System;
using System.Web;
namespace ViewBagExample.Models
{
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
}
}
2. Create HomeController
using System;
using System.Web.Mvc;
using ViewBagExample.Models;

namespace ViewBagExample.Controllers
{
public class HomeController : Controller
{

List<Student> studentList = new List<Student>() {


new Student(){ StudentID=1, StudentName="Steve", Age = 21 },
new Student(){ StudentID=2, StudentName="Bill", Age = 25 },
new Student(){ StudentID=3, StudentName="Ram", Age = 20 },
new Student(){ StudentID=4, StudentName="Ron", Age = 31 },
new Student(){ StudentID=5, StudentName="Rob", Age = 19 }
};
//
// GET: /Home/

public ActionResult Index()


{
ViewBag.TotalStudents = studentList.Count();
return View();
}

}
}

3. Create View

@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<label>Total Students:</label> @ViewBag.TotalStudents

ViewData Example
1. Create Model Student

using System;
using System.Web;

namespace ViewDataExample.Models
{
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
}
}
2. Create Home Controller

using System;
using ViewDataExample.Models;

namespace ViewDataExample.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/

public ActionResult Index()


{
List<Student> studentList = new List<Student>();
studentList.Add(new Student() { StudentName = "Bill" });
studentList.Add(new Student() { StudentName = "Steve" });
studentList.Add(new Student() { StudentName = "Ram" });

ViewData["students"] = studentList;

return View();

}
}

3. Create View
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<ul>
@foreach (var std in ViewData["students"] as
List<ViewDataExample.Models.Student>)
{
<li>
@std.StudentName
</li>
}
</ul>

TempData Example

1. Create HomeController
using System;
using System.Web.Mvc;

namespace TempDataDemo.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
TempData["name"] = "Test data";
TempData["age"] = 30;
return RedirectToAction("About");
}
public ActionResult About()
{

if (TempData.ContainsKey("name"))
{
string userName = TempData["name"].ToString();
}

if (TempData.ContainsKey("age"))
{
int userAge = int.Parse(TempData["age"].ToString());
}

// do something with userName or userAge here

return RedirectToAction("Contact");
}
public ActionResult Contact()
{
string data;

if (TempData["name"] != null)
data = TempData["name"] as string;

return View();
}
}
}

Put debug point and check that the value of TempData


in Contact action method. Data will be null.

Example of TempData
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TempDataDemo.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
[HttpGet]
public ActionResult Index()
{
TempData["name"] = "Test data";
TempData["age"] = 30;
return View("Index");
}

[HttpGet]
public ActionResult About()
{
if (TempData.ContainsKey("name"))
{
string userName = TempData["name"].ToString();
}
if (TempData.ContainsKey("age"))
{
int userAge = int.Parse(TempData["age"].ToString());
}
// do something with userName or userAge here
TempData.Keep();
return View();
}
[HttpPost]
public ActionResult About1()
{
string data;
if (TempData["name"] != null)
data = TempData["name"] as string;
return View();
}

public ActionResult Contact()


{

return View();
}
}
}

2. Create View For Index

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<input type="submit" value="Click To Next"


onclick="location.href='@Url.Action("About", "Home")'" />

3. Create View For About

@{
ViewBag.Title = "About";
}
@using (Html.BeginForm("About1", "Home", FormMethod.Post))
{
<h2>About</h2>
<table>
<tr>
<td>Name</td>
<td>@TempData["name"]</td>
</tr>
<tr>
<td>Age</td>
<td>@TempData["Age"]</td>
</tr>
<tr>

<td><input type="submit" value="Click To Next" /></td>


</tr>
</table>
}

4. Create View For About1


@{
ViewBag.Title = "About1";
}
<h2>About1</h2>

<table>
<tr>
<td>Name</td>
<td>@TempData["name"]</td>
</tr>

</table>

You might also like