Académique Documents
Professionnel Documents
Culture Documents
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);
}
}
}
@{
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
{
}
}
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/
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());
}
return RedirectToAction("Contact");
}
public ActionResult Contact()
{
string data;
if (TempData["name"] != null)
data = TempData["name"] as string;
return View();
}
}
}
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();
}
return View();
}
}
}
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@{
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>
<table>
<tr>
<td>Name</td>
<td>@TempData["name"]</td>
</tr>
</table>