Action Verb Example
1. Create Model Student
using System;
using [Link];
namespace [Link]
{
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 [Link];
using [Link];
namespace [Link]
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Student s)
{
[Link]=[Link];
[Link]=[Link];
[Link]=[Link];
[Link]=[Link];
[Link]=[Link];
return View(s);
}
}
}
3. Create Strongly type View
@model [Link]
@{
[Link] = "Index";
}
<h2>Student Registration</h2>
@if (@[Link] != null)
{
<h2>Roll No : @[Link]</h2>
<h2>Name : @[Link]</h2>
<h2>Email : @[Link]</h2>
<h2>Address : @[Link]</h2>
<h2>City : @[Link] </h2>
}
@using (@[Link]("Index", "Home"))
{
<table>
<tr>
<td>Roll No</td>
<td>@[Link](Model => [Link])</td>
</tr>
<tr>
<td>Name</td>
<td>@[Link](Model => [Link])</td>
</tr>
<tr>
<td>Email</td>
<td>@[Link](Model => [Link])</td>
</tr>
<tr>
<td>Address</td>
<td>@[Link](Model => [Link])</td>
</tr>
<tr>
<td>City</td>
<td>@[Link](Model => [Link])</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 [Link];
namespace [Link]
{
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
}
}
2. Create HomeController
using System;
using [Link];
using [Link];
namespace [Link]
{
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()
{
[Link] = [Link]();
return View();
}
}
}
3. Create View
@{
[Link] = "Index";
}
<h2>Index</h2>
<label>Total Students:</label> @[Link]
ViewData Example
1. Create Model Student
using System;
using [Link];
namespace [Link]
{
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 [Link];
namespace [Link]
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
List<Student> studentList = new List<Student>();
[Link](new Student() { StudentName = "Bill" });
[Link](new Student() { StudentName = "Steve" });
[Link](new Student() { StudentName = "Ram" });
ViewData["students"] = studentList;
return View();
}
}
3. Create View
@{
[Link] = "Index";
}
<h2>Index</h2>
<ul>
@foreach (var std in ViewData["students"] as
List<[Link]>)
{
<li>
@[Link]
</li>
}
</ul>
TempData Example
1. Create HomeController
using System;
using [Link];
namespace [Link]
{
public class HomeController : Controller
{
public ActionResult Index()
{
TempData["name"] = "Test data";
TempData["age"] = 30;
return RedirectToAction("About");
}
public ActionResult About()
{
if ([Link]("name"))
{
string userName = TempData["name"].ToString();
}
if ([Link]("age"))
{
int userAge = [Link](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 [Link];
using [Link];
using [Link];
using [Link];
namespace [Link]
{
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 ([Link]("name"))
{
string userName = TempData["name"].ToString();
}
if ([Link]("age"))
{
int userAge = [Link](TempData["age"].ToString());
}
// do something with userName or userAge here
[Link]();
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
@{
[Link] = "Index";
}
<h2>Index</h2>
<input type="submit" value="Click To Next"
onclick="[Link]='@[Link]("About", "Home")'" />
3. Create View For About
@{
[Link] = "About";
}
@using ([Link]("About1", "Home", [Link]))
{
<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
@{
[Link] = "About1";
}
<h2>About1</h2>
<table>
<tr>
<td>Name</td>
<td>@TempData["name"]</td>
</tr>
</table>