In this article I will explain with an example, how to assign (set) value to TextBox using TempData in ASP.Net MVC Razor.
This article will illustrate how to use TempData to set value in TextBox created using Html.TextBox helper function in ASP.Net MVC Razor.
Controller
The Controller consists of the following Action method.
Action method for handling GET operation
Inside this Action method, a string value is set into a TempData object i.e. Name and simply the View is returned.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
TempData["Name"] = "Mudassar Khan";
return View();
}
}
View
The View consists of a TextBox created using Html.TextBox helper function. The TempData object is passed as the second parameter i.e. the value of the TextBox.
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
@Html.TextBox("Name", TempData["Name"])
</body>
</html>
Downloads