2010-05-20 81 views
3

有了这个剧本我从JsonResult(GetDevicesTable)数据初学者的问题,并把他们的表(ID = “OrderDevices”)与发布数据表JsonResult

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

当点击在提交我需要这样的事情:

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

我有这个脚本的问题,因为不知道如何发布数据到这个JsonResult:

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(); 
    } 
+0

当您点击提交按钮时,您想使用哪个'Id','DeviceId'和'OrderId'?已经插入最后一个表格行的那个? – 2010-05-20 07:07:26

+0

我想要所有的(Id,deviceId和OrderId)。他们需要在JsonResult DevicesActions – Ognjen 2010-05-20 07:16:28

+0

我需要在JsonResult中发布所有表格行 – Ognjen 2010-05-20 07:17:08

回答

2

为了让所有的idsdeviceIdsorderIds从表中您将需要修改的操作签名,以便它可以接受数组:

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

现在,你有这样的地方,你可以发送AJAX查询:

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

,并用于映射getValue功能:

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

什么是第一个孩子,第n个孩子(2)... – Ognjen 2010-05-20 09:08:03

+0

允许您获得'n的'n'孩子'td' '。请参阅文档:http://api.jquery.com/nth-child-selector/ – 2010-05-20 09:30:54