As for my below code i am not able to get sectionID from tr, i need to get dynamic id of sectionID on each delete button click but it is always giving me null
至于我的下面的代码,我无法从tr获取sectionID,我需要在每个删除按钮单击时获取sectionID的动态ID,但它总是给我null
Here is the Jquery Script :
这是Jquery脚本:
<script>
$(function () {
$('.btnDelete').click(function () {
var sectionID = $(this).closest('tr').find('.sectionID');
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: 'CheckSectionIDagainststudentID',
data: JSON.stringify({ sectionID: sectionID.html() }),
success: function (data) {
if (data == true) {
$.alert("Cannot delete | Students exsisting in this
Section!");
}
else if (data == false) {
var secCode = JSON.parse(this.data);
var code = sectionid['sectionid'];
window.location.href = "SectionDelete?Page=data§ionid="
+ code;
}
},
failure: function (response) {
$('#result').html(response);
}
});
});
});
</script>
and here is Razor Page
这是Razor Page
@foreach (var item in Model)
{
<tr class="sectionID">
<td >
@Html.DisplayFor(modelItem => item.sectionID)
</td>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td class="secCode">
<button style="width:49.5%" ID="Button2" type="button" onclick="location.href='@Url.Action("SectionEdit", "Section",new { id = item.sectionID, name = item.name })'">Edit</button>
<button style="width:49.5%" ID="deletebtn" runat="server" type="button" onclick="location.href='@Url.Action("SectionDelete", "Section",new { id = item.sectionID, name = item.name })'">Delete</button>
<button class="btnDelete">Delete</button>
</td>
</tr>
}
This is Controller Which i need to pass data to
这是我需要传递数据的控制器
[HttpPost]
public ActionResult CheckSectionIDagainststudentID(string sectionID)
{
return Json(sectionID);
}
2 个解决方案
#1
0
As per your question you are not able to get value from var sectionID = $(this).closest('tr').find('.sectionID');
therefore here is a way you can achieve your result
根据你的问题,你无法从var sectionID = $(this).closest('tr')。find('.sectionID');因此,这是一种可以实现结果的方法
//Your Dynamic Button should look like this
//in value Bind your sectionID @ MVC
<button class="btnDelete" value="5" onclick="AjaxDelete(this)">Delete</button>
//For Function
function AjaxDelete(values) {
//Write this in Ajax
//Fetch Value from attribute value
var sectionID = $(values).attr("value"); // you will get 5 as value
//Your Ajax Call with SectionId as Parameter
}
Edit : as you have got value U can split the value by below code where string is your var of sectionID
编辑:因为你有价值你可以通过下面的代码将值拆分,其中string是你的sectionID的var
if ((~string.indexOf('\n '))) {
string= string.split('\n ')[1].split('\n')[0];
}
#2
0
Use string array :
使用字符串数组:
[HttpPost]
public ActionResult CheckSectionIDagainststudentID(string[] sectionID)
{
return Json(sectionID);
}
#1
0
As per your question you are not able to get value from var sectionID = $(this).closest('tr').find('.sectionID');
therefore here is a way you can achieve your result
根据你的问题,你无法从var sectionID = $(this).closest('tr')。find('.sectionID');因此,这是一种可以实现结果的方法
//Your Dynamic Button should look like this
//in value Bind your sectionID @ MVC
<button class="btnDelete" value="5" onclick="AjaxDelete(this)">Delete</button>
//For Function
function AjaxDelete(values) {
//Write this in Ajax
//Fetch Value from attribute value
var sectionID = $(values).attr("value"); // you will get 5 as value
//Your Ajax Call with SectionId as Parameter
}
Edit : as you have got value U can split the value by below code where string is your var of sectionID
编辑:因为你有价值你可以通过下面的代码将值拆分,其中string是你的sectionID的var
if ((~string.indexOf('\n '))) {
string= string.split('\n ')[1].split('\n')[0];
}
#2
0
Use string array :
使用字符串数组:
[HttpPost]
public ActionResult CheckSectionIDagainststudentID(string[] sectionID)
{
return Json(sectionID);
}