how to append a session value to a textbox in html please help me out. I am getting the value into the session but how assign it to a textbox in html i want to display the name according to the id below is the code..
Controller Class for displaying Name According to the id
[HttpGet]
public ActionResult GetDetailsById()
{
BusinessEntities be = new BusinessEntities();
return View(be);
}
[HttpPost]
public ActionResult DetailsById(FormCollection fc)
{
BusinessEntities be = new BusinessEntities();
be.id =Convert.ToInt32( fc["id"]);
DataAceessLayer dal = new DataAceessLayer();
dal.GetDetails(be);
TempData["Name"] = be.name1;
return View(test1());
}
Data Acess Layer for the controller
public void GetDetails(BusinessEntities be)
{
be.name1 = HttpContext.Current.Session.ToString();
be.gender1 = HttpContext.Current.Session.ToString();
be.course1 = HttpContext.Current.Session.ToString();
be.grade1 = HttpContext.Current.Session.ToString();
using(SqlConnection con=new SqlConnection(GlobalConnection.connection))
{
con.Open();
SqlCommand cmd = new SqlCommand("GETDETAILSBYID", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", be.id);
SqlDataReader dr = cmd.ExecuteReader();
if(dr.HasRows)
{
while(dr.Read())
{
be.name1 = dr["name"].ToString();
be.gender1 = dr["gender"].ToString();
be.course1 = dr["course"].ToString();
be.grade1 = dr["grade"].ToString();
}
}
}
view for this:
<body>
@using (Html.BeginForm("DetailsById", "EmployeeController", FormMethod.Post))
{
if(Session["name"]==null)
{
<table>
<tr>
<td>Id</td>
<td>
<input type="text" name="txtid" id="txtid" required />
</td>
</tr>
<tr>
<td>Name</td>
<td>
<input type="text" name="txtname" id="txtname" value="@TempData["Name"]" required />
</td>
</tr>
<tr>
<td>Gender</td>
<td>
<input type="text" name="txtgender" id="txtgender" value="@Model.gender" required />
</td>
</tr>
<tr>
<td>Course</td>
<td>
<input type="text" name="txtcourse" id="txtcourse" value="@Model.course" required />
</td>
</tr>
<tr>
<td>grade</td>
<td>
<input type="text" name="txtngrade" id="txtgrade" value="@Model.grade" required />
</td>
</tr>
<tr>
<td colspan="2">
<input type="button" value="GetDetails" id="btnGetDetails" onclick="GetDetails()" />
</td>
</tr>
<tr>
<td>
<input type="hidden" id="txthidden" name="txthidden" value="@Model.name1" />
</td>
</tr>
</table>
}
}
<script type="text/javascript">
function GetDetails() {
if($("#btnGetDetails").val()=="GetDetails")
{
$.ajax({
type: 'post',
url: '@Url.Action("DetailsById")',
data: {
id: $("#txtid").val(),
},
datatype: 'json',
success: function (data) {
$("#txtname").val() = @TempData["Name"].ToString();
}
})
}
}
</script>
Please HElp me i want to append and dispaly the name according to the session value