2010-05-20 82 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