简体   繁体   English

如何在asp.net mvc中获取html表值?

[英]How to get the html table value in asp.net mvc?

Here i am a dragging a table(1) row and dropping it in another table(2) using asp.net mvc similar to this one http://www.redips.net/javascript/drag-and-drop-table-row/ and the values are from database .It is working fine and what i need is the html table values (ie the values inside the td) of the table(2).how can i get those values and save it in dataset in asp.net MVC.Here is my code 在这里我拖动一个表(1)行并将其放在另一个表(2)中使用类似于这个的asp.net mvc http://www.redips.net/javascript/drag-and-drop-table-row /和值来自数据库。它工作正常,我需要的是表(2)的html表值(即td内的值)。如何获取这些值并将其保存在asp中的数据集中。 net MVC.Here是我的代码

<div id="drag">
    <table class="tbl1">
        <tr>
            <th colspan="2">
                Table 1
            </th>
        </tr>
        <% foreach (var item1 in Model)
    { %>
        <tr class="rl">
            <td class="rowhandler">
                <div class="drag row">
                    <%= Html.Encode(item1.Id) %>
                </div>
            </td>
            <td class="rowhandler">
                <div class="drag row">
                    <%= Html.Encode(item1.Title) %>
                </div>
            </td>
        </tr>
        <% } %>
        <tr style="display: none;">
            <td colspan="2">
                <span>Message line</span>
            </td>
        </tr>
    </table>
    <table id="tab" runat="server">
        <tr>
            <th colspan="2" class="mark">
                Table 2
            </th>
        </tr>
        <tr class="rd">
            <td>
            </td>
            <td>
            </td>
        </tr>
        <tr style="display: none;">
            <td colspan="2">
                <span>Message line</span>
            </td>
        </tr>
    </table>
</div>

Any suggesion?I am new to MVC 有什么建议吗?我是MVC的新手

Use jQuery ajax to save the data in server. 使用jQuery ajax将数据保存在服务器中。 You can make a call to a controller action from your jquery with your data and it can save the data to your tables. 您可以使用数据从jquery调用控制器操作,它可以将数据保存到表中。

  var mydata="";  /// read your table cells values and store it in this string

  $.post("yourcontroller/action/", { data : mydata} ,function(response){
        //do something with the response from action
  });

As Brian already suggested, traversing the DOM maybe a bad idea. 正如Brian已经建议的那样,遍历DOM可能是一个坏主意。 A better way would be to store the table2 data in a javascript array (whenever a drop event occurs add to the array, or remove from the array when cells are dragged off the table). 更好的方法是将table2数据存储在javascript数组中(每当发生一个drop事件添加到数组中,或者当单元格从表中拖出时从数组中删除)。 Then have an ajax call to submit the whole array in one go. 然后有一个ajax调用一次提交整个数组。

If I am understanding this correctly when the page loads you have the data for both tables in the database. 如果我在页面加载时正确理解这一点,那么您将获得数据库中两个表的数据。 You can just keep a working set of the changes being made to the table and update those records accordingly. 您可以保留对表所做更改的一组工作,并相应地更新这些记录。 Add logic to the "drop" event in your JS to store the item's ID and it's new position. 在JS中的“drop”事件中添加逻辑,以存储项目的ID及其新位置。

This saves you from having to update unaffected records and also from traversing the entirety of both tables on submit. 这使您无需更新未受影响的记录,也无需在提交时遍历两个表的全部内容。

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

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