Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
[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
C#
public void GetDetails(BusinessEntities be)
      {
          //string name;

        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.id = Convert.ToInt32( dr["id"].ToString());
                     be.name1 = dr["name"].ToString();
                     be.gender1 = dr["gender"].ToString();
                     be.course1 = dr["course"].ToString();
                     be.grade1 = dr["grade"].ToString();
                  }
              }
          }

view for this:
HTML
<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">

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
Posted
Updated 24-Feb-15 19:45pm
v2

1 solution

You try like this i think it will work


$("#txtname").val('@TempData["Name"].ToString()');
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900