通过在控制器中发布字符串来制作 json



大家好,我目前有一个我需要这张桌子的例子


<table class="table table-bordered" width="100%" cellspacing="0" id="tableID">

  <thead>

    <tr>

      <th>A</th>

      <th>B</th>

      <th>C</th>

      <th>D</th>

      <th>E</th>

    </tr>

    </thead>

    <tbody>

    <tr>

      <td align="center">val1</td>

      <td align="center">val2</td>

      <td align="center">val3</td>

      <td align="center">1500</td>

      <td align="center" class="myID"><input type="number" name="txtID" class="txtID" oninput="setValueAttr(this)" value="" min="0" max="1000"></td>

    </tr>

    <tr>

      <td align="center">val1</td>

      <td align="center">val2</td>

      <td align="center">val3</td>

      <td align="center">1500</td>

      <td align="center" class="myID"><input type="number" name="txtID" class="txtID" oninput="setValueAttr(this)"  value="" min="0" max="1000"></td>

    </tr>

    <tr>

      <td align="center">val1</td>

      <td align="center">val2</td>

      <td align="center">val3</td>

      <td align="center">1500</td>

      <td align="center" class="myID"><input type="number" name="txtID" class="txtID" oninput="setValueAttr(this)"  value="" min="0" max="1000" ></td>

    </tr>

  </tbody>

</table>

<form>

<button type="button" onclick="aplicar()">Aplicar</button>

</form>

<script>

function setValueAttr(el){

  el.setAttribute('value', el.value)

}

</script>

我看到了一种在我的控制器中接收这个 json 的方法,就像这样


public class tableData

{

    public string A { get; set; }

    public string B { get; set; }

    public string C { get; set; }

    public string D { get; set; }

    public string E { get; set; }

}



public void View(List<tableData> tableDatas)

{

    var t = tableDatas;


}

但是,我需要在我的控制器中执行与此 javascript 类似的操作。


var total = [];

for (i = 0; i < tableData.length; i++) {

    total[i] = "&num_operacion" + (i + 1) + "=" + tableData[i].A +

        "&monto" + (i + 1) + "=" + tableData[i].E +

        "&num_documento" + (i + 1) + "=" + tableData[i].B +

        "&tipo_documento" + (i + 1) + "=" + tableData[i].C

}

我已经使用该 javascript 完成了此操作并使用 post 发送字符串,但如果字符串足够大,ajax 将崩溃


慕斯王
浏览 145回答 2
2回答

慕森卡

[FromBody]ModelName在您的操作方法中使用助手将其绑定到您的预期模型public IActionResult([FromBody]List<MyModel> model){............}

函数式编程

需要制作一个模型而不是在控制器内做类所以..模型.cs&nbsp; &nbsp; public class tableData&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string A { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string B { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string C { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string D { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string E { get; set; }&nbsp; &nbsp; }并将控制器更改为此&nbsp; &nbsp; &nbsp; &nbsp; [HttpGet]&nbsp; &nbsp; &nbsp; &nbsp; public ActionResult Index()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return View();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; [HttpPost]&nbsp; &nbsp; &nbsp; &nbsp; public JsonResult Index(List<tableData> tableDatas)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<string> total = new List<string>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < tableDatas.Count(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total.Add($"&num_operacion{i+1}={tableDatas[i].A}&monto{i+1}={tableDatas[i].E}&num_documento{i + 1}={tableDatas[i].B}&tipo_documento{i + 1}={tableDatas[i].C}");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Json(total);&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript