In this article I will explain with an example, how to assign Session value to Html.TextBox function in ASP.Net MVC.
 
 
Controller
The Controller consists of the following Action method.
Action method for handling GET operation
Inside this Action method, the Session objects are set and the View is returned.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        Session["Name"] = "Mudassar Khan";
        Session["Country"] = "India";
        return View();
    }
}
 
 
View
Inside the View, there are two TextBoxes. The TextBox for the Name value is a simple HTML INPUT element while the TextBox for the Country value is created using Html.TextBox helper function.
For the HTML INPUT element, the Session object is set in the value attribute using the Razor Syntax while for the other TextBox, the Session object is passed to the value parameter of the Html.TextBox helper function.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <table cellpadding="5">
        <tr>
            <td>Name:</td>
            <td><input type="text" value="@Session["Name"]" /></td>
        </tr>
        <tr>
            <td>Country:</td>
            <td>@Html.TextBox("txtMessage", Session["Country"])</td>
        </tr>
    </table>
</body>
</html>
 
 
Screenshot
Assign Session value to Html.TextBox function in ASP.Net MVC
 
 
Downloads