Вот рабочая демонстрация, как показано ниже:
1.Модель:
public class CorpDetail
{
public int SAMID { get; set; }
public int CorporateID { get; set; }
public string CorporateName { get; set; }
public string BUName { get; set; }
public int TotalAmount { get; set; }
public string approver1 { get; set; }
public string approver3 { get; set; }
public string approver4 { get; set; }
public string approver5 { get; set; }
}
2.Просмотр:
@model IEnumerable<CorpDetail>
<table>
<tr>
<th></th>
<th>SAMID</th>
<th>CorporateID</th>
<th>CorporateName</th>
<th>BUName</th>
<th>TotalAmount</th>
<th>approver1</th>
<th>approver3</th>
<th>approver4</th>
<th>approver5</th>
</tr>
@{
var i = 0;
@foreach (var list in Model)
{
<tr>
<td>
<input type="button" id="@i" value="Select" class="btn btn-info" onclick="test(@i)" /></td>
<td id="SAMID">@list.SAMID</td>
<td id="CorporateID">@list.CorporateID</td>
<td id="CorporateName">@list.CorporateName</td>
<td id="BUName">@list.BUName</td>
<td id="TotalAmount">@list.TotalAmount</td>
<td id="approver1">@list.approver1</td>
<td id="approver3">@list.approver3</td>
<td id="approver4">@list.approver4</td>
<td id="approver5">@list.approver5</td>
</tr>
i++;
}
}
</table>
@section Scripts{
<script>
function test(i) {
var sAMID =parseInt($('#' + i).closest('tr').children('#SAMID').text());
var corporateID = parseInt($('#' + i).closest('tr').children('#CorporateID').text());
var corporateName = $('#' + i).closest('tr').children('#CorporateName').text();
var bUName = $('#' + i).closest('tr').children('#BUName').text();
var totalAmount = parseInt($('#' + i).closest('tr').children('#TotalAmount').text());
var approver1 = $('#' + i).closest('tr').children('#approver1').text();
var approver3 = $('#' + i).closest('tr').children('#approver3').text();
var approver4 = $('#' + i).closest('tr').children('#approver4').text();
var approver5 = $('#' + i).closest('tr').children('#approver5').text();
var data = {sAMID,corporateID,corporateName,bUName,totalAmount,approver1,approver3,approver4,approver5}
$.ajax({
type: "POST",
url: "/Home/Contact",
contentType:"application/json",
data: JSON.stringify(data),
sucess: function (data) {
//...
}
})
}
</script>
}
3.Контроллер:
public IActionResult Contact()
{
var model = new List<CorpDetail>()
{
new CorpDetail(){ SAMID =1,CorporateID=1,CorporateName="aa",BUName="aa1",TotalAmount=10,approver1="a1",approver3="a3",approver4="a4",approver5="a5"},
new CorpDetail(){ SAMID =2,CorporateID=2,CorporateName="bb",BUName="bb1",TotalAmount=20,approver1="b1",approver3="b3",approver4="b4",approver5="b5"},
new CorpDetail(){ SAMID =3,CorporateID=3,CorporateName="cc",BUName="cc1",TotalAmount=30,approver1="c1",approver3="c3",approver4="c4",approver5="c5"},
};
return View(model);
}
[HttpPost]
public IActionResult Contact([FromBody]CorpDetail corpDetail)
{
ViewData["Message"] = "Your contact page.";
return View();
}
4.Результат: