英文:
Getting value value from textbox inside loop with c#
问题
我在我的项目中有一个表格,我循环遍历这个表格中的行(trs
)。表格中有文本框。当我进行更改并想要保存时,它基于第一条记录执行操作。我分享了这个图像。
上图中的第二条记录 "toptan" 我已经更改了,但传递给我的记录是 "Perakendee"。因此,我的代码总是获取第一条记录的值。有没有解决这个问题的方法?或者更合理的编码方式?
HTML 代码
<table id="kullanicilistesi" class="table mb-3 table-center">
<thead>
<tr>
<th class="border-bottom text-start py-3">Şirket Adı</th>
<th class="border-bottom text-start py-3">Database (Veri Tabanı)</th>
<th class="border-bottom text-center py-3">Şube</th>
<th class="border-bottom text-center py-3">Diğer</th>
</tr>
</thead>
<tbody>
@foreach (var item in AdminStatic.GetIsletme()) {
<tr>
<td class="">
<input type="text" id="isletmeadi" class="form-control" value="@item.IsletmeAdi" />
</td>
<td class=""> <input type="text" id="veritabani" class="form-control" value="@item.VeriTabani" /></td>
<td class=""> <input type="text" id="sube" class="form-control" value="@item.Sube" /></td>
<td class="">
<a style="color:limegreen;" href="javascript:void(0)" data-idd="@item.ID" onclick="IsletmeDuzenle(this)">
<i class="uil uil-check"></i>
</a>
<a style="color: rgb(217, 37, 37);" href="javascript:void(0)" onclick="IsletmeSil(@item.ID)">
<i class="uil uil-trash"></i>
</a>
</td>
</tr>
}
</tbody>
</table>
JavaScript 代码
function IsletmeDuzenle(t) {
var id = $(t).data('idd');
alert($("#isletmeadi").val());
getValue($baseUrl + "/B2BAdmin/IsletmeDuzenle",
{ id: id, isletmeAdi: $("#isletmeadi").val(), veritabani: $("#veritabani").val(), sube: $("#sube").val() },
function (data, err) {
if (data=="Basarili") {
Swal.fire({
title: 'Başarılı',
html: 'İşletme Başarıyla Güncellenmiştir',
icon: 'success'
}).then(function (result) {
window.location.reload();
});
}
})
}
请注意,这只是代码和文本的翻译,没有其他内容。
英文:
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
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
<table id="kullanicilistesi" class="table mb-3 table-center">
<thead>
<tr>
<th class="border-bottom text-start py-3">Şirket Adı</th>
<th class="border-bottom text-start py-3">Database (Veri Tabanı)</th>
<th class="border-bottom text-center py-3">Şube</th>
<th class="border-bottom text-center py-3">Diğer</th>
</tr>
</thead>
<tbody>
@foreach (var item in AdminStatic.GetIsletme()) {
<tr>
<td class="">
<input type="text" id="isletmeadi" class="form-control" value="@item.IsletmeAdi" />
</td>
<td class=""> <input type="text" id="veritabani" class="form-control" value="@item.VeriTabani" /></td>
<td class=""> <input type="text" id="sube" class="form-control" value="@item.Sube" /></td>
<td class="">
<a style="color:limegreen;" href`your text`="javascript:void(0)" data-idd="@item.ID" onclick="IsletmeDuzenle(this)">
<i class="uil uil-check"></i>
</a>
<a style="color: rgb(217, 37, 37); " href="javascript:void(0)" onclick="IsletmeSil(@item.ID)">
<i class="uil uil-trash"></i>
</a>
</td>
</tr>
}
</tbody>
</table>
Javascript Codes
function IsletmeDuzenle(t) {
var id = $(t).data('idd');
alert($("#isletmeadi").val());
getValue($baseUrl + "/B2BAdmin/IsletmeDuzenle",
{ id: id, isletmeAdi: $("#isletmeadi").val(), veritabani: $("#veritabani").val(), sube: $("#sube").val() },
function (data, err) {
if (data=="Basarili") {
Swal.fire({
title: 'Başarılı',
html: 'İşletme Başarıyla Güncellenmiştir',
icon: 'success'
}).then(function (result) {
window.location.reload();
});
}
})
}
答案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
以下是代码部分的翻译:
function goGetThem(them) {
console.log("Called:", them);
return; // just to not do the call
getValue(them.getUrl, them.callParameters,
function(data, err) {
if (data == "Basarili") {
Swal.fire({
title: 'Başarılı',
html: 'İşletme Başarıyla Güncellenmiştir',
icon: 'success'
}).then(function(result) {
window.location.reload();
});
}
});
}
$('.green-container')
.on('click', '.go-green-button', function(event) {
console.log('check clicked');
const $editRow = $(this).closest('.business-container');
const $tableContainer = $(event.delegateTarget);
console.log($editRow.find('.database-name').val());
const baseurl = $tableContainer.data("baseurl");
const them = {
getUrl: baseurl + "/B2BAdmin/IsletmeDuzenle",
callParameters: {
id: $(this).data('idd'),
isletmeAdi: $editRow.find(".isletmeadi").val(),
veritabani: $editRow.find(".veritabani").val(),
sube: $editRow.find(".sube").val()
}
};
goGetThem(them);
}).on('click', ".remove-button", function(event) {
let mytd = $(this).closest('td');
mytd.toggleClass("delete-me");
// for fun we just toggle the data and make it "pretty" on clicks
$(this).closest('.business-container').get(0).dataset.todelete = mytd.hasClass('delete-me') ? "yes" : "no";
//console.log('Go remove:', this.innerHTML);
});
.remove-button.btn {
color: #D92525;
}
.go-green.button {
color: #32CD32;
}
.delete-me {
border: 1px solid red;
}
.business-container:not([data-todelete]) {
background-color: #22222204;
}
.business-container[data-todelete="yes"] {
background-color: #FF000008;
}
.business-container[data-todelete="no"] {
background-color: #00FF0008;
}
.link-container .uil {
font-size: 1.5rem;
border: solid 1px lime;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.2.2/font/bootstrap-icons.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<table class="table mb-3 table-center green-container" data-baseurl="/fun/urlforgreen/">
<thead>
<tr>
<th class="border-bottom text-start py-3">Şirket Adı</th>
<th class="border-bottom text-start py-3">Database (Veri Tabanı)</th>
<th class="border-bottom text-center py-3">Şube</th>
<th class="border-bottom text-center py-3">Diğer</th>
</tr>
</thead>
<tbody class="container">
<tr class="container container-row business-container">
<td><input type="text" class="form-control business-name isletmeadi" value="Happy business" /></td>
<td><input type="text" class="form-control database-name veritabani" value="Fun Database" /></td>
<td><input type="text" class="form-control sube" value="Branch5" /></td>
<td class="link-container">
<a class="go-green-button btn" href="#" data-idd="branch-5">
<i class="uil bi-check"></i>
</a>
<a class="remove-button btn" href="#" data-idd="branch-5">
<i class="uil bi-trash"></i>
</a>
</td>
</tr>
<tr class="container container-row business-container">
<td><input type="text" class="form-control business-name isletmeadi" value="Car business" /></td>
<td><input type="text" class="form-control database-name veritabani" value="Car Database" /></td>
<td><input type="text" class="form-control sube" value="branch454" /></td>
<td class="link-container">
<a class="go-green-button btn" href="#" data-idd="row-454">
<i class="uil bi-check"></i>
</a>
<a class="remove-button btn" href="#" data-idd="row-454">
<i class="uil bi-trash"></i>
</a>
</td>
</tr>
</tbody>
</table>
<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=""
just to have a cleaner visual of the HTML.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function goGetThem(them) {
console.log("Called:", them);
return; // just to not do the call
getValue(them.getUrl, them.callParameters,
function(data, err) {
if (data == "Basarili") {
Swal.fire({
title: 'Başarılı',
html: 'İşletme Başarıyla Güncellenmiştir',
icon: 'success'
}).then(function(result) {
window.location.reload();
});
}
});
}
$('.green-container')
.on('click', '.go-green-button', function(event) {
console.log('check clicked');
const $editRow = $(this).closest('.business-container');
const $tableContainer = $(event.delegateTarget);
console.log($editRow.find('.database-name').val());
const baseurl = $tableContainer.data("baseurl");
const them = {
getUrl: baseurl + "/B2BAdmin/IsletmeDuzenle",
callParameters: {
id: $(this).data('idd'),
isletmeAdi: $editRow.find(".isletmeadi").val(),
veritabani: $editRow.find(".veritabani").val(),
sube: $editRow.find(".sube").val()
}
};
goGetThem(them);
}).on('click', ".remove-button", function(event) {
let mytd = $(this).closest('td');
mytd.toggleClass("delete-me");
// for fun we just toggle the data and make it "pretty" on clicks
$(this).closest('.business-container').get(0).dataset.todelete = mytd.hasClass('delete-me') ? "yes" : "no";
//console.log('Go remove:', this.innerHTML);
});
<!-- language: lang-css -->
.remove-button.btn {
color: #D92525;
}
.go-green.button {
color: #32CD32;
}
.delete-me {
border: 1px solid red;
}
.business-container:not([data-todelete]) {
background-color: #22222204;
}
.business-container[data-todelete="yes"] {
background-color: #FF000008;
}
.business-container[data-todelete="no"] {
background-color: #00FF0008;
}
.link-container .uil {
font-size: 1.5rem;
border: solid 1px lime;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.2.2/font/bootstrap-icons.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<table class="table mb-3 table-center green-container" data-baseurl="/fun/urlforgreen/">
<thead>
<tr>
<th class="border-bottom text-start py-3">Şirket Adı</th>
<th class="border-bottom text-start py-3">Database (Veri Tabanı)</th>
<th class="border-bottom text-center py-3">Şube</th>
<th class="border-bottom text-center py-3">Diğer</th>
</tr>
</thead>
<tbody class="container">
<tr class="container container-row business-container">
<td><input type="text" class="form-control business-name isletmeadi" value="Happy business" /></td>
<td><input type="text" class="form-control database-name veritabani" value="Fun Database" /></td>
<td><input type="text" class="form-control sube" value="Branch5" /></td>
<td class="link-container">
<a class="go-green-button btn" href="#" data-idd="branch-5">
<i class="uil bi-check"></i>
</a>
<a class="remove-button btn" href="#" data-idd="branch-5">
<i class="uil bi-trash"></i>
</a>
</td>
</tr>
<tr class="container container-row business-container">
<td><input type="text" class="form-control business-name isletmeadi" value="Car business" /></td>
<td><input type="text" class="form-control database-name veritabani" value="Car Database" /></td>
<td><input type="text" class="form-control sube" value="branch454" /></td>
<td class="link-container">
<a class="go-green-button btn" href="#" data-idd="row-454">
<i class="uil bi-check"></i>
</a>
<a class="remove-button btn" href="#" data-idd="row-454">
<i class="uil bi-trash"></i>
</a>
</td>
</tr>
</tbody>
</table>
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论