从C#循环内获取文本框中的值

huangapple go评论88阅读模式
英文:

Getting value value from textbox inside loop with c#

问题

我在我的项目中有一个表格,我循环遍历这个表格中的行(trs)。表格中有文本框。当我进行更改并想要保存时,它基于第一条记录执行操作。我分享了这个图像。

从C#循环内获取文本框中的值

上图中的第二条记录 "toptan" 我已经更改了,但传递给我的记录是 "Perakendee"。因此,我的代码总是获取第一条记录的值。有没有解决这个问题的方法?或者更合理的编码方式?

HTML 代码

  1. <table id="kullanicilistesi" class="table mb-3 table-center">
  2. <thead>
  3. <tr>
  4. <th class="border-bottom text-start py-3">Şirket Adı</th>
  5. <th class="border-bottom text-start py-3">Database (Veri Tabanı)</th>
  6. <th class="border-bottom text-center py-3">Şube</th>
  7. <th class="border-bottom text-center py-3">Diğer</th>
  8. </tr>
  9. </thead>
  10. <tbody>
  11. @foreach (var item in AdminStatic.GetIsletme()) {
  12. <tr>
  13. <td class="">
  14. <input type="text" id="isletmeadi" class="form-control" value="@item.IsletmeAdi" />
  15. </td>
  16. <td class=""> <input type="text" id="veritabani" class="form-control" value="@item.VeriTabani" /></td>
  17. <td class=""> <input type="text" id="sube" class="form-control" value="@item.Sube" /></td>
  18. <td class="">
  19. <a style="color:limegreen;" href="javascript:void(0)" data-idd="@item.ID" onclick="IsletmeDuzenle(this)">
  20. <i class="uil uil-check"></i>
  21. </a>
  22. <a style="color: rgb(217, 37, 37);" href="javascript:void(0)" onclick="IsletmeSil(@item.ID)">
  23. <i class="uil uil-trash"></i>
  24. </a>
  25. </td>
  26. </tr>
  27. }
  28. </tbody>
  29. </table>

JavaScript 代码

  1. function IsletmeDuzenle(t) {
  2. var id = $(t).data('idd');
  3. alert($("#isletmeadi").val());
  4. getValue($baseUrl + "/B2BAdmin/IsletmeDuzenle",
  5. { id: id, isletmeAdi: $("#isletmeadi").val(), veritabani: $("#veritabani").val(), sube: $("#sube").val() },
  6. function (data, err) {
  7. if (data=="Basarili") {
  8. Swal.fire({
  9. title: 'Başarılı',
  10. html: 'İşletme Başarıyla Güncellenmiştir',
  11. icon: 'success'
  12. }).then(function (result) {
  13. window.location.reload();
  14. });
  15. }
  16. })
  17. }

请注意,这只是代码和文本的翻译,没有其他内容。

英文:

I have a table in my project, I loop the trs in this table. There are textboxes in the table. When I make changes and want to save, it takes action based on the first record. I share the image

从C#循环内获取文本框中的值

The second record in the picture above "toptan" I changed it to. but the record that came to me "Perakendee"
So my code always gets the value of the first record. Is there a way to solve this? Or a more Logical coding

HTML codes

  1. &lt;table id=&quot;kullanicilistesi&quot; class=&quot;table mb-3 table-center&quot;&gt;
  2. &lt;thead&gt;
  3. &lt;tr&gt;
  4. &lt;th class=&quot;border-bottom text-start py-3&quot;&gtirket Adı&lt;/th&gt;
  5. &lt;th class=&quot;border-bottom text-start py-3&quot;&gt;Database (Veri Tabanı)&lt;/th&gt;
  6. &lt;th class=&quot;border-bottom text-center py-3&quot;&gtube&lt;/th&gt;
  7. &lt;th class=&quot;border-bottom text-center py-3&quot;&gt;Diğer&lt;/th&gt;
  8. &lt;/tr&gt;
  9. &lt;/thead&gt;
  10. &lt;tbody&gt;
  11. @foreach (var item in AdminStatic.GetIsletme()) {
  12. &lt;tr&gt;
  13. &lt;td class=&quot;&quot;&gt;
  14. &lt;input type=&quot;text&quot; id=&quot;isletmeadi&quot; class=&quot;form-control&quot; value=&quot;@item.IsletmeAdi&quot; /&gt;
  15. &lt;/td&gt;
  16. &lt;td class=&quot;&quot;&gt; &lt;input type=&quot;text&quot; id=&quot;veritabani&quot; class=&quot;form-control&quot; value=&quot;@item.VeriTabani&quot; /&gt;&lt;/td&gt;
  17. &lt;td class=&quot;&quot;&gt; &lt;input type=&quot;text&quot; id=&quot;sube&quot; class=&quot;form-control&quot; value=&quot;@item.Sube&quot; /&gt;&lt;/td&gt;
  18. &lt;td class=&quot;&quot;&gt;
  19. &lt;a style=&quot;color:limegreen;&quot; href`your text`=&quot;javascript:void(0)&quot; data-idd=&quot;@item.ID&quot; onclick=&quot;IsletmeDuzenle(this)&quot;&gt;
  20. &lt;i class=&quot;uil uil-check&quot;&gt;&lt;/i&gt;
  21. &lt;/a&gt;
  22. &lt;a style=&quot;color: rgb(217, 37, 37); &quot; href=&quot;javascript:void(0)&quot; onclick=&quot;IsletmeSil(@item.ID)&quot;&gt;
  23. &lt;i class=&quot;uil uil-trash&quot;&gt;&lt;/i&gt;
  24. &lt;/a&gt;
  25. &lt;/td&gt;
  26. &lt;/tr&gt;
  27. }
  28. &lt;/tbody&gt;
  29. &lt;/table&gt;

Javascript Codes

  1. function IsletmeDuzenle(t) {
  2. var id = $(t).data(&#39;idd&#39;);
  3. alert($(&quot;#isletmeadi&quot;).val());
  4. getValue($baseUrl + &quot;/B2BAdmin/IsletmeDuzenle&quot;,
  5. { id: id, isletmeAdi: $(&quot;#isletmeadi&quot;).val(), veritabani: $(&quot;#veritabani&quot;).val(), sube: $(&quot;#sube&quot;).val() },
  6. function (data, err) {
  7. if (data==&quot;Basarili&quot;) {
  8. Swal.fire({
  9. title: &#39;Başarılı&#39;,
  10. html: &#39;İşletme Başarıyla G&#252;ncellenmiştir&#39;,
  11. icon: &#39;success&#39;
  12. }).then(function (result) {
  13. window.location.reload();
  14. });
  15. }
  16. })
  17. }

答案1

得分: 1

我不太懂土耳其语,但看起来是尝试更新或删除行。在复选标记上:点击="IsletmeDuzenle(this)" 在垃圾桶上:点击="IsletmeSil(@item.ID)"。

我猜测可能是复选标记的行为不符合您的期望。如果是用于更新,您可以像垃圾桶一样传递ID。如果所有行都已导入,只需更改和/或删除,我认为最好的方法是创建一个函数,该函数接受该条目的ID。但是,即使您不显示它,您仍然需要在该对象中拥有ID。

因此,对于复选标记:点击="somefunction(@item.ID)"。

英文:

I'm not good with turkish but it looks like trying to update or delete rows.
On checkmark: onclick="IsletmeDuzenle(this)"
On trashcan: onclick="IsletmeSil(@item.ID)"

I'm guessing its the checkmark that is not behaving as you want it to. If it's for an update, you could pass the ID like with the trashcan.
If all the rows are imported and only need to change and/or get deleted, I think a function that does that that takes the ID of the entry is the best way. But then you need to have the ID in that object, even if you don't show it.

So,
On checkmark: onclick="somefunction(@item.ID)"

答案2

得分: 1

以下是代码部分的翻译:

  1. function goGetThem(them) {
  2. console.log("Called:", them);
  3. return; // just to not do the call
  4. getValue(them.getUrl, them.callParameters,
  5. function(data, err) {
  6. if (data == "Basarili") {
  7. Swal.fire({
  8. title: 'Başarılı',
  9. html: 'İşletme Başarıyla Güncellenmiştir',
  10. icon: 'success'
  11. }).then(function(result) {
  12. window.location.reload();
  13. });
  14. }
  15. });
  16. }
  17. $('.green-container')
  18. .on('click', '.go-green-button', function(event) {
  19. console.log('check clicked');
  20. const $editRow = $(this).closest('.business-container');
  21. const $tableContainer = $(event.delegateTarget);
  22. console.log($editRow.find('.database-name').val());
  23. const baseurl = $tableContainer.data("baseurl");
  24. const them = {
  25. getUrl: baseurl + "/B2BAdmin/IsletmeDuzenle",
  26. callParameters: {
  27. id: $(this).data('idd'),
  28. isletmeAdi: $editRow.find(".isletmeadi").val(),
  29. veritabani: $editRow.find(".veritabani").val(),
  30. sube: $editRow.find(".sube").val()
  31. }
  32. };
  33. goGetThem(them);
  34. }).on('click', ".remove-button", function(event) {
  35. let mytd = $(this).closest('td');
  36. mytd.toggleClass("delete-me");
  37. // for fun we just toggle the data and make it "pretty" on clicks
  38. $(this).closest('.business-container').get(0).dataset.todelete = mytd.hasClass('delete-me') ? "yes" : "no";
  39. //console.log('Go remove:', this.innerHTML);
  40. });
  1. .remove-button.btn {
  2. color: #D92525;
  3. }
  4. .go-green.button {
  5. color: #32CD32;
  6. }
  7. .delete-me {
  8. border: 1px solid red;
  9. }
  10. .business-container:not([data-todelete]) {
  11. background-color: #22222204;
  12. }
  13. .business-container[data-todelete="yes"] {
  14. background-color: #FF000008;
  15. }
  16. .business-container[data-todelete="no"] {
  17. background-color: #00FF0008;
  18. }
  19. .link-container .uil {
  20. font-size: 1.5rem;
  21. border: solid 1px lime;
  22. }
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.2.2/font/bootstrap-icons.css">
  3. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
  4. <table class="table mb-3 table-center green-container" data-baseurl="/fun/urlforgreen/">
  5. <thead>
  6. <tr>
  7. <th class="border-bottom text-start py-3">Şirket Adı</th>
  8. <th class="border-bottom text-start py-3">Database (Veri Tabanı)</th>
  9. <th class="border-bottom text-center py-3">Şube</th>
  10. <th class="border-bottom text-center py-3">Diğer</th>
  11. </tr>
  12. </thead>
  13. <tbody class="container">
  14. <tr class="container container-row business-container">
  15. <td><input type="text" class="form-control business-name isletmeadi" value="Happy business" /></td>
  16. <td><input type="text" class="form-control database-name veritabani" value="Fun Database" /></td>
  17. <td><input type="text" class="form-control sube" value="Branch5" /></td>
  18. <td class="link-container">
  19. <a class="go-green-button btn" href="#" data-idd="branch-5">
  20. <i class="uil bi-check"></i>
  21. </a>
  22. <a class="remove-button btn" href="#" data-idd="branch-5">
  23. <i class="uil bi-trash"></i>
  24. </a>
  25. </td>
  26. </tr>
  27. <tr class="container container-row business-container">
  28. <td><input type="text" class="form-control business-name isletmeadi" value="Car business" /></td>
  29. <td><input type="text" class="form-control database-name veritabani" value="Car Database" /></td>
  30. <td><input type="text" class="form-control sube" value="branch454" /></td>
  31. <td class="link-container">
  32. <a class="go-green-button btn" href="#" data-idd="row-454">
  33. <i class="uil bi-check"></i>
  34. </a>
  35. <a class="remove-button btn" href="#" data-idd="row-454">
  36. <i class="uil bi-trash"></i>
  37. </a>
  38. </td>
  39. </tr>
  40. </tbody>
  41. </table>
  42. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>

希望这些翻译对您有所帮助。

英文:

Just to illustrate you can do this without id's and have multiple I did a fake mock-up and some ugly colors with some event handlers NOT embedded in the HTML.
I also removed empty class=&quot;&quot; just to have a cleaner visual of the HTML.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. function goGetThem(them) {
  2. console.log(&quot;Called:&quot;, them);
  3. return; // just to not do the call
  4. getValue(them.getUrl, them.callParameters,
  5. function(data, err) {
  6. if (data == &quot;Basarili&quot;) {
  7. Swal.fire({
  8. title: &#39;Başarılı&#39;,
  9. html: &#39;İşletme Başarıyla G&#252;ncellenmiştir&#39;,
  10. icon: &#39;success&#39;
  11. }).then(function(result) {
  12. window.location.reload();
  13. });
  14. }
  15. });
  16. }
  17. $(&#39;.green-container&#39;)
  18. .on(&#39;click&#39;, &#39;.go-green-button&#39;, function(event) {
  19. console.log(&#39;check clicked&#39;);
  20. const $editRow = $(this).closest(&#39;.business-container&#39;);
  21. const $tableContainer = $(event.delegateTarget);
  22. console.log($editRow.find(&#39;.database-name&#39;).val());
  23. const baseurl = $tableContainer.data(&quot;baseurl&quot;);
  24. const them = {
  25. getUrl: baseurl + &quot;/B2BAdmin/IsletmeDuzenle&quot;,
  26. callParameters: {
  27. id: $(this).data(&#39;idd&#39;),
  28. isletmeAdi: $editRow.find(&quot;.isletmeadi&quot;).val(),
  29. veritabani: $editRow.find(&quot;.veritabani&quot;).val(),
  30. sube: $editRow.find(&quot;.sube&quot;).val()
  31. }
  32. };
  33. goGetThem(them);
  34. }).on(&#39;click&#39;, &quot;.remove-button&quot;, function(event) {
  35. let mytd = $(this).closest(&#39;td&#39;);
  36. mytd.toggleClass(&quot;delete-me&quot;);
  37. // for fun we just toggle the data and make it &quot;pretty&quot; on clicks
  38. $(this).closest(&#39;.business-container&#39;).get(0).dataset.todelete = mytd.hasClass(&#39;delete-me&#39;) ? &quot;yes&quot; : &quot;no&quot;;
  39. //console.log(&#39;Go remove:&#39;, this.innerHTML);
  40. });

<!-- language: lang-css -->

  1. .remove-button.btn {
  2. color: #D92525;
  3. }
  4. .go-green.button {
  5. color: #32CD32;
  6. }
  7. .delete-me {
  8. border: 1px solid red;
  9. }
  10. .business-container:not([data-todelete]) {
  11. background-color: #22222204;
  12. }
  13. .business-container[data-todelete=&quot;yes&quot;] {
  14. background-color: #FF000008;
  15. }
  16. .business-container[data-todelete=&quot;no&quot;] {
  17. background-color: #00FF0008;
  18. }
  19. .link-container .uil {
  20. font-size: 1.5rem;
  21. border: solid 1px lime;
  22. }

<!-- language: lang-html -->

  1. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  2. &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap-icons@1.2.2/font/bootstrap-icons.css&quot;&gt;
  3. &lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot; crossorigin=&quot;anonymous&quot;&gt;
  4. &lt;table class=&quot;table mb-3 table-center green-container&quot; data-baseurl=&quot;/fun/urlforgreen/&quot;&gt;
  5. &lt;thead&gt;
  6. &lt;tr&gt;
  7. &lt;th class=&quot;border-bottom text-start py-3&quot;&gtirket Adı&lt;/th&gt;
  8. &lt;th class=&quot;border-bottom text-start py-3&quot;&gt;Database (Veri Tabanı)&lt;/th&gt;
  9. &lt;th class=&quot;border-bottom text-center py-3&quot;&gtube&lt;/th&gt;
  10. &lt;th class=&quot;border-bottom text-center py-3&quot;&gt;Diğer&lt;/th&gt;
  11. &lt;/tr&gt;
  12. &lt;/thead&gt;
  13. &lt;tbody class=&quot;container&quot;&gt;
  14. &lt;tr class=&quot;container container-row business-container&quot;&gt;
  15. &lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;form-control business-name isletmeadi&quot; value=&quot;Happy business&quot; /&gt;&lt;/td&gt;
  16. &lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;form-control database-name veritabani&quot; value=&quot;Fun Database&quot; /&gt;&lt;/td&gt;
  17. &lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;form-control sube&quot; value=&quot;Branch5&quot; /&gt;&lt;/td&gt;
  18. &lt;td class=&quot;link-container&quot;&gt;
  19. &lt;a class=&quot;go-green-button btn&quot; href=&quot;#&quot; data-idd=&quot;branch-5&quot;&gt;
  20. &lt;i class=&quot;uil bi-check&quot;&gt;&lt;/i&gt;
  21. &lt;/a&gt;
  22. &lt;a class=&quot;remove-button btn&quot; href=&quot;#&quot; data-idd=&quot;branch-5&quot;&gt;
  23. &lt;i class=&quot;uil bi-trash&quot;&gt;&lt;/i&gt;
  24. &lt;/a&gt;
  25. &lt;/td&gt;
  26. &lt;/tr&gt;
  27. &lt;tr class=&quot;container container-row business-container&quot;&gt;
  28. &lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;form-control business-name isletmeadi&quot; value=&quot;Car business&quot; /&gt;&lt;/td&gt;
  29. &lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;form-control database-name veritabani&quot; value=&quot;Car Database&quot; /&gt;&lt;/td&gt;
  30. &lt;td&gt;&lt;input type=&quot;text&quot; class=&quot;form-control sube&quot; value=&quot;branch454&quot; /&gt;&lt;/td&gt;
  31. &lt;td class=&quot;link-container&quot;&gt;
  32. &lt;a class=&quot;go-green-button btn&quot; href=&quot;#&quot; data-idd=&quot;row-454&quot;&gt;
  33. &lt;i class=&quot;uil bi-check&quot;&gt;&lt;/i&gt;
  34. &lt;/a&gt;
  35. &lt;a class=&quot;remove-button btn&quot; href=&quot;#&quot; data-idd=&quot;row-454&quot;&gt;
  36. &lt;i class=&quot;uil bi-trash&quot;&gt;&lt;/i&gt;
  37. &lt;/a&gt;
  38. &lt;/td&gt;
  39. &lt;/tr&gt;
  40. &lt;/tbody&gt;
  41. &lt;/table&gt;
  42. &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot; integrity=&quot;sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月9日 22:00:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定