我需要获取已检查 Finalize 的每个表行的行 ID,并将该字符串/json 发布到 ASP.net MVC Controller 。 Ajax 给我带来了问题。我成功抓取了需要提交的数据;但我不确定最好的方法是什么。我是否需要创建一个 json 对象然后发布该对象,或者是否有一种简单的方法可以发布我所拥有的内容?我尝试了 json 路线,但没有走得太远,所以我回到了这个,这至少获取了正确的数据。
finalize = function() {
var tableData = "";
$('#results').find('tr').each(function() {
var row = $(this);
if (row.find('input[type="checkbox"]').is(':checked')) {
tableData = tableData + $(this).find('td:first').text() + '\n'; // get ID
}
});
//var myRows = JSON.parse(tableData);
//$.post("/Journal/SaveEntry", { Row: myRows });
alert(tableData);
};
<script src="https://github.com/maxazan/jquery-treegrid/blob/master/js/jquery.treegrid.js"></script>
<script src="https://github.com/maxazan/jquery-treegrid/blob/master/js/jquery.treegrid.bootstrap3.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<table class="table tree truncate_table">
<tbody>
<tr class=" info">
<th>Row ID</th>
<th>...</th>
<th>Finalize</th>
</tr>
<tr class="treegrid-1">
<td>
<span class="treegrid-expander"></span>
R4915
</td>
<td>...</td>
<td>
<input type="checkbox" name="Finalize" value="Finalize">
</td>
</tr>
<tr class="treegrid-2">
<td>
<span class="treegrid-expander"></span>
<a target="_blank" href="http://example.com" class="vt-p">
R4942
</a>
</td>
<td>...</td>
<td>
<input type="checkbox" name="Finalize" value="Finalize">
</td>
</tr>
</tbody>
</table>
<button onclick="finalize()" type="button" class="btn btn-danger pull-right">Finalize</button>
最佳答案
我在这里假设返回 ID 的集合比您需要在 Controller 上拆分的字符串更有用
HTML
<button type="button" id="finalize" class="btn btn-danger pull-right">Finalize</button>
脚本
$('#finalize').click(function() {
var idArray = new Array();
$('#results').find('tr').each(function() {
var row = $(this);
if (row.find('input[type="checkbox"]').is(':checked')) {
idArray.push(row.find('td:first').text());
}
});
$.ajax({
url: '@Url.Action("SaveEntry", "Journal")';
type: 'post'
traditional: true,
data: { values: idArray }
})
});
Controller
public ActionResult SaveEntry(string[] values) // or int[] values if the ID is typeof int
{
}
关于javascript - Ajax提交到asp.net mvc post操作或将集合转换为json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27160000/