使用此脚本,我从JsonResult(GetDevicesTable)获取数据并将它们放到表中(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>
<% } %>
点击提交时我需要这样的东西:
$(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 :(得分:2)
为了从表中获取所有ids
,deviceIds
和orderIds
,您需要修改操作签名,以便它可以接受数组:
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());
}