ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Update record in HTML Table using ActionLink in ASP.Net MVC<p>Hi&nbsp;alya14,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <p>I have made use of the following table Customers with the schema as follows.</p> <p><img src="https://www.aspsnippets.com/Handlers/DownloadFile.ashx?File=f18ac914-bc9b-437a-88e2-bd640ce05282.png" alt="" width="384" height="137" /></p> <p>I have already inserted few records in the table.</p> <p><img src="https://www.aspsnippets.com/Handlers/DownloadFile.ashx?File=b736972b-595c-4656-ab75-976e054877c7.png" alt="" width="340" height="156" /></p> <p>You can download the database table SQL by clicking the download link below.</p> <p><a href="https://www.aspsnippets.com/DownloadFile.aspx?File=Customers_Table_AutoIncrement.sql">Download SQL file</a></p> <p><strong><span style="text-decoration: underline;">Model</span></strong></p> <pre class="brush: csharp">public class Customer { public int Id { get; set; } public string Name { get; set; } public string Country { get; set; } }</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <pre class="brush: csharp">using System.Configuration; using System.Data.SqlClient;</pre> <p><strong><span style="text-decoration: underline;">Controller</span></strong></p> <pre class="brush: csharp">public class HomeController : Controller { // GET: /Home/ public ActionResult Index() { List&lt;Customer&gt; customers = GetCustomers(); return View(customers); } private static List&lt;Customer&gt; GetCustomers() { List&lt;Customer&gt; customers = new List&lt;Customer&gt;(); string conString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; string query = &#34;SELECT * FROM Customers&#34;; using (SqlConnection con = new SqlConnection(conString)) { using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; con.Open(); SqlDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { customers.Add(new Customer { Id = Convert.ToInt32(sdr[&#34;CustomerId&#34;]), Name = sdr[&#34;Name&#34;].ToString(), Country = sdr[&#34;Country&#34;].ToString() }); } con.Close(); } } return customers; } [HttpPost] public ActionResult UpdateCustomer(int id, string country) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString); con.Open(); SqlCommand cmd = new SqlCommand(&#34;SELECT COUNT(*) FROM Customers WHERE CustomerId=@P1&#34;, con); cmd.Parameters.AddWithValue(&#34;@P1&#34;, id); int sonuc = Convert.ToInt32(cmd.ExecuteScalar()); con.Close(); if (sonuc &gt;= 1) { con.Open(); cmd = new SqlCommand(&#34;UPDATE Customers SET Country=@P2 WHERE CustomerId=@P1&#34;, con); cmd.Parameters.AddWithValue(&#34;@P1&#34;, id); cmd.Parameters.AddWithValue(&#34;@P2&#34;, country); cmd.ExecuteNonQuery(); con.Close(); } return new EmptyResult(); } }</pre> <p><strong><span style="text-decoration: underline;">View</span></strong></p> <pre class="brush: html">&lt;%@ Page Language=&#34;C#&#34; Inherits=&#34;System.Web.Mvc.ViewPage&lt;IEnumerable&lt;_ActionLink_Update_Record_MVC.Models.Customer&gt;&gt;&#34; %&gt; &lt;!DOCTYPE html PUBLIC &#34;-//W3C//DTD XHTML 1.0 Transitional//EN&#34; &#34;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#34;&gt; &lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;Index&lt;/title&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css&#34; /&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { $(&#39;.Update&#39;).on(&#34;click&#34;, function () { var id = $(this).closest(&#39;tr&#39;).find(&#39;td&#39;).eq(0).html().trim(); var country = $(this).closest(&#39;tr&#39;).find(&#39;#txtCountry&#39;).val(); var custromer = {}; custromer.id = id; custromer.country = country; $.ajax({ type: &#34;POST&#34;, url: &#34;/Home/UpdateCustomer&#34;, data: custromer, dataType: &#34;json&#34; }); return false; }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;table id=&#34;myTable&#34; class=&#34;table table-striped table-bordered table-condensed&#34;&gt; &lt;thead&gt; &lt;tr&gt; &lt;th style=&#34;width: 2px&#34;&gt;Id&lt;/th&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Country&lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;% foreach (var customer in Model) { %&gt; &lt;tr&gt; &lt;td&gt;&lt;%: customer.Id%&gt;&lt;/td&gt; &lt;td&gt;&lt;%: customer.Name%&gt;&lt;/td&gt; &lt;td&gt;&lt;%:Html.TextBoxFor(t =&gt; customer.Country, new { id = &#34;txtCountry&#34;, @class = &#34;form-control&#34;, style = &#34;width:150px&#34; })%&gt;&lt;/td&gt; &lt;td&gt;&lt;%:Html.ActionLink(&#34;UPDATE&#34;, &#34;UpdateManageUser&#34;, &#34;Home&#34;, new { @class = &#34;Update btn btn-primary pull-left&#34; })%&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% } %&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </pre> <p><strong><span style="text-decoration: underline;">Screenshots</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/6I7TXWw.gif" alt="" width="401" height="232" /></span></strong></p> <p><span style="text-decoration: underline;">Record in database after update</span></p> <p><span style="text-decoration: underline;"><img src="https://i.imgur.com/ovGdzPR.jpg" alt="" width="252" height="143" /></span></p>https://www.aspforums.net:443/Threads/426671/Update-record-in-HTML-Table-using-ActionLink-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/426671/Update-record-in-HTML-Table-using-ActionLink-in-ASPNet-MVC/Tue, 11 Jun 2019 03:28:03 GMTUpdate record in HTML Table using ActionLink in ASP.Net MVC<p>this is now return id value like below</p> <pre class="brush: js">var Id = $(this).closest(&#39;tr&#39;).find(&#39;td&#39;).eq(0).html().trim(); &lt;input id=&#34;customer_Id&#34; name=&#34;customer.Id&#34; style=&#34;width:100%&#34; type=&#34;text&#34; value=&#34;3&#34;&gt;&#34;</pre> <p>When use</p> <pre class="brush: html">&lt;td&gt;@Html.TextBoxFor(i =&gt; customer.Id, new {&nbsp;style = &#34;width:100%&#34; })&lt;/td&gt;</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/426671/Update-record-in-HTML-Table-using-ActionLink-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/426671/Update-record-in-HTML-Table-using-ActionLink-in-ASPNet-MVC/Tue, 11 Jun 2019 06:13:36 GMTUpdate record in HTML Table using ActionLink in ASP.Net MVC<blockquote><cite>alya14 says:</cite> <pre>var Id = $(this).closest('tr').find('td').eq(0).html().trim();</pre> </blockquote> <p>&nbsp;Change the above line with&nbsp;</p> <pre class="brush: js">var id = $(this).closest(&#39;tr&#39;).find(&#39;#Id&#39;).val();</pre> <p>and assign id property to the textbox like below.</p> <pre class="brush: html">&lt;td&gt;@Html.TextBoxFor(i =&gt; customer.Id, new { id = &#34;Id&#34;, style = &#34;width:100%&#34; })&lt;/td&gt;</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/426671/Update-record-in-HTML-Table-using-ActionLink-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/426671/Update-record-in-HTML-Table-using-ActionLink-in-ASPNet-MVC/Tue, 11 Jun 2019 06:35:59 GMT