Update record in HTML Table using ActionLink in ASP.Net MVC

Last Reply 5 months ago By dharmendr

Posted 5 months ago

hi I listed the value from database and I want to update password filed after listed , my scenario like below

        <table id="myTable" class="table table-striped table-bordered table-condensed">
            <thead>
                <tr>
                    <th style="width: 2px">Id</th>                    
                    <th>Name And Surname</th>
                    <th>Passwords</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var veri in Model)
                {
                <tr>
                    <td>@veri.Id</td>                    
                    <td>@veri.UserName</td>
                    <td>@Html.TextBoxFor(t => new AccountInfo().Pwd, new { @Value = @veri.Pwd, @class = "form-control", style = "width:100px"})
                    <td>                 
                      @Html.ActionLink(linkText: "UPDATE", actionName: "UpdateManageUser", controllerName: "AdminPanel", routeValues: new {id = @veri.Id, NewPwd = @veri.NewPwd },
                      htmlAttributes: new {onclick= "userUpdateJson()", id = "btnUpdate",@class = "btn btn-primary pull-left"})
                    </td>
                </tr>
                }
            </tbody>
        </table>

 

        public ActionResult UpdateManageUser(int id,string pwd)
        {
            var con = Core.GetLocalConnection();
            con.Open();

            var cmd = new SqlCommand("SELECT COUNT(*) FROM S_ACCOUNTINFO WHERE ID=@P1", con);
            cmd.Parameters.AddWithValue("@P1", id);
            var sonuc = Convert.ToInt32(cmd.ExecuteScalar());
            if (sonuc == 1)
            {
                cmd = new SqlCommand("UPDATE S_ACCOUNTINFO SET PWD=@P2 WHERE ID=@P1", con);
                cmd.Parameters.AddWithValue("@P1", id);
                cmd.Parameters.AddWithValue("@P2", pwd);
                cmd.ExecuteNonQuery();
            }
            con.Close();          

            return RedirectToAction("ManageUser");
            
        }

 

You are viewing reply posted by: dharmendr 5 months ago.
Posted 5 months ago
alya14 says:
var Id = $(this).closest('tr').find('td').eq(0).html().trim();

 Change the above line with 

var id = $(this).closest('tr').find('#Id').val();

and assign id property to the textbox like below.

<td>@Html.TextBoxFor(i => customer.Id, new { id = "Id", style = "width:100%" })</td>