javascript - Ajax提交到asp.net mvc post操作或将集合转换为json?

标签 javascript jquery asp.net-mvc

我需要获取已检查 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/

相关文章:

c# - MVC 5 数据注释 "Not Equal to Zero"

javascript - 如何在 for 循环中使用 $.each 或替代方案

javascript - 如何在 React Native 项目中使用 Node.JS 中的 Crypto 内置模块?

javascript - $httpBackend.expect 与 $httpBackend.when

javascript - 克隆元素并在 jquery 循环内进行修改将永远执行

jquery - widgetVar 不能在 p :fileUpload 中使用

jquery - Selenium 单击按钮

javascript - 使用 Jquery.valid() 在 Asp.net MVC 中进行多表单验证

asp.net-mvc - Jstree - 通过用户输入将节点添加到树(单击按钮)

javascript - axios spread() 回调参数数量未知