Initial issue with sending data table to JsonResult

With this script, I get data from JsonResult (GetDevicesTable) and put it in a table (id = "OrderDevices")

 <script type="text/javascript">

$(document).ready(function() {

    $("#getDevices a").click(function() {
        var Id = $(this).attr("rel");
        var rowToMove = $(this).closest("tr");
        $.post("/ImportXML/GetDevicesTable", { Id: Id }, function(data) {
        if (data.success) {
            //remove row
            rowToMove.fadeOut(function() { $(this).remove() });
            //add row to other table
            $("#OrderDevices").append("<tr><td>"+ data.DeviceId+ "</td><td>" + data.Id+  "</td><td>" + data.OrderId +  "</td></tr>");                    
            } else {
                alert(data.ErrorMsg);
            }
        }, "json");
    });

});

   <% using (Html.BeginForm()) {%>
   <table id="OrderDevices" class="data-table">
   <tr>

        <th>
            DeviceId
        </th>
        <th>
            Id
        </th>
        <th>
            OrderId
        </th>
    </tr>
   </table>
         <p>
            <input type="submit" value="Submit" />
        </p>
 <% } %>

When I click Submit, I need something like this:

    $(document).ready(function() {

    $("#submit").click(function() {
        var Id = $(this).attr("rel");
        var DeviceId = $(this).attr(???);
        var OrderId = $(this).attr(???);
        var rowToMove = $(this).closest("tr");
        $.post("/ImportXML/DevicesActions", { Id: Id, DeviceId:DeviceId, OrderId:OrderId }, function(data) {

        }, "json");
    });
});

I have a problem with this script because I don't know how the mail data for this JsonResult is:

    public JsonResult DevicesActions(int Id,int DeviceId,int OrderId)
    {
     orderdevice ord = new orderdevice();
            ord.Id = Id;
            ord.OrderId = DeviceId;
            ord.DeviceId = OrderId;

            DBEntities.AddToorderdevice(ord);
            DBEntities.SaveChanges();
    }
+3
source share
1 answer

To get everything ids, deviceIdsand orderIdsfrom the table, you will need to change the signature of the action so that it can accept arrays:

public ActionResult DevicesActions(int[] ids, int[] deviceIds, int[] orderIds)
{
    ...
}

Now that you have this place, you can send an AJAX request:

var deviceIds = $('#OrderDevices tr td:first-child').map(getValue);
var ids = $('#OrderDevices tr td:nth-child(2)').map(getValue);
var orderIds = $('#OrderDevices tr td:nth-child(3)').map(getValue);
$.post(
    '/ImportXML/DevicesActions', 
    {
        deviceIds: deviceIds,
        ids: ids,
        orderIds: orderIds
    }, 
    function(json) {
        // success
    }
);

and getValuewhich are used to display:

function getValue() {
    return parseInt($(this).text());
}
+2
source

Source: https://habr.com/ru/post/1746321/


All Articles