简体   繁体   English

发送 html 数据以查看,然后发送到控制器以更新 sql 数据库

[英]sending html data to view and then sending to controller to update sql database

I have a HTML table where one of the columns value is dynamically added.我有一个 HTML 表,其中一个列值是动态添加的。 I have an update button, upon clicking it I want this data to get updated in my sql database.我有一个更新按钮,单击它后,我希望这些数据在我的 sql 数据库中得到更新。 For this, I am planning to first fetch the table data and put into view , then send data to controller and then updating sql.为此,我计划先获取表数据并放入视图,然后将数据发送到控制器,然后更新 sql。

I am stuck at the first step,我被困在第一步,
Descibing table below下表说明

              <thead>
               <tr>
                  <th>ID</th>
                  <th >Name</th>
                  <th>Active</th>
                  <th>Order By</th>
               </tr>
             </thead>
                              <tbody>
                                  @if (ViewBag.data != null)
                                  {
                                      foreach (var item in ViewBag.data)
                                      {
                                          <tr>
                                              <td >@item.AutoID</td>
                                              <td @item.Text</td>
                                              <td >@item.Active</td>
                                              <td>@item.OrderBy</td>

                                          </tr>

                                      }

                                  }




                              </tbody>

                          </table>

                      </div>
                  </div>
                  <input type="submit" value="Update Preference" class="BtnUpdOrderId" />

              </div>

I tried this below js function to fetch the data我在js函数下面尝试了这个来获取数据


 $(".BtnUpdOrderId").click(function () {
        
        var tr = $(this).closest('tr');
        var id = tr.find('input[name="autoid"]').val();
        var text = tr.find('input[name="text"]').val();
        var active = tr.find('input[name="active"]').val();
        var orderby = tr.find('input[name="orderby"]').val();
        alert('type1 : ' + id + ' ' + text + ' ' + active + ' ' + active);

    });

but not sure why nothing came in alert但不知道为什么没有警报

var TableData = new Array();
    $('#tblLookup1 tr').each(function (row, tr) {
        TableData = TableData + $(tr).find('td:eq(0)').text();
        alert(TableData);
    });

then tried the above block of code to get data in a variable but still not able to get anything.然后尝试上面的代码块来获取变量中的数据,但仍然无法获取任何内容。

Once I get the data I can try sending from view->controller.一旦我得到数据,我可以尝试从视图->控制器发送。

So need the following help:所以需要以下帮助:

  1. what mistake am I making?我犯了什么错误?
  2. once this is fixed, how to send data to sql?一旦解决了这个问题,如何将数据发送到 sql? (this is a ado.net based mvc project) (这是一个基于 ado.net 的 mvc 项目)

you might want to consider creating a json object: Creating json object in mvc and returning from controller您可能要考虑创建一个 json 对象: 在 mvc 中创建 json 对象并从控制器返回

then build your table Convert JSON array to an HTML table in jQuery然后构建您的表将 JSON 数组转换为 jQuery 中的 HTML 表

finally, the update need only post back the json object最后,更新只需要回传 json 对象

https://dontpaniclabs.com/blog/post/2013/02/27/posting-json-data-to-an-mvc-controller-via-ajax/ https://dontpaniclabs.com/blog/post/2013/02/27/posting-json-data-to-an-mvc-controller-via-ajax/

if you going to use this jason object make sure you use serialization如果你要使用这个 jason 对象,请确保你使用序列化

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-6-0 https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-6-0

your have to patch these concepts together but there are a lots of tutorials and examples online so it be a good learning experience您必须将这些概念拼凑在一起,但是网上有很多教程和示例,因此这是一次很好的学习体验

I hope this helps我希望这有帮助

helpful links: https://www.sqlshack.com/modifying-json-data-using-json_modify-in-sql-server/ Updating a JSON object using Javascript https://www.geeksforgeeks.org/how-to-convert-json-data-to-a-html-table-using-javascript-jquery/ create json object from html table using selected colums using jquery有用的链接: https : //www.sqlshack.com/modifying-json-data-using-json_modify-in-sql-server/ 使用 Javascript 更新 JSON 对象https://www.geeksforgeeks.org/how-to-convert -json-data-to-a-html-table-using-javascript-jquery/ 使用 jquery 选择的列从 html 表创建 json 对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM