亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過在控制器中發布字符串來制作 json

通過在控制器中發布字符串來制作 json

慕斯王 2022-01-07 14:04:48
大家好,我目前有一個我需要這張桌子的例子<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 將崩潰
查看完整描述

2 回答

?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

[FromBody]ModelName在您的操作方法中使用助手將其綁定到您的預期模型


public IActionResult([FromBody]List<MyModel> model)

{

............

}


查看完整回答
反對 回復 2022-01-07
?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

需要制作一個模型而不是在控制器內做類所以..


模型.cs


    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; }

    }

并將控制器更改為此


        [HttpGet]

        public ActionResult Index()

        {

            return View();

        }

        [HttpPost]

        public JsonResult Index(List<tableData> tableDatas)

        {

            List<string> total = new List<string>();


            for(int i = 0; i < tableDatas.Count(); i++)

            {

                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}");

            }


            return Json(total);

        }


查看完整回答
反對 回復 2022-01-07
  • 2 回答
  • 0 關注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號