gpt4 book ai didi

c# - 使用 Jquery Ajax 将模型和 2 个复选框列表发布到 Controller

转载 作者:行者123 更新时间:2023-12-03 12:00:41 25 4
gpt4 key购买 nike

我想使用 JQuery Ajax 从用作部分 View 的 EditorTemplate 发送 2 个包含选中复选框值的列表以及我的模型。这是:

@model EsdpExport.View_Models.ProductLineCreateViewModel
@using EsdpHelpers

<script type="text/javascript">

$('#add-list').click(function () {

var commoditiesToRemove = $('input[type="checkbox"].removeCheckbox:checked').map(function () {
return $(this).val();
}).toArray();
var commoditiesToAdd = $('input[type="checkbox"].addCheckbox:checked').map(function () {
return $(this).val();
}).toArray();

$.ajax({
type: 'POST',
url: { @Url.Action("UpdateCommodityList", "ProductLine") },
contentType: "application/json; application/json; application/json",
data: { availableCommoditiesToAdd: commoditiesToAdd,
currentCommoditiesToRemove: commoditiesToRemove,
@Model.ToJson() },
success: function (result) {
$('#CommodityTable').html(result);
},
});

return false;
});

$('#remove-list').click(function () {

var commoditiesToRemove = $('input[type="checkbox"].removeCheckbox:checked').map(function () {
return $(this).val();
}).toArray();
var commoditiesToAdd = $('input[type="checkbox"].addCheckbox:checked').map(function () {
return $(this).val();
}).toArray();

$.ajax({
type: 'POST',
url: { @Url.Action("UpdateCommodityList", "ProductLine") },
contentType: "application/json; application/json; application/json",
data: {
availableCommoditiesToAdd: commoditiesToAdd,
currentCommoditiesToRemove: commoditiesToRemove,
@Model.ToJson() },
success: function (result) {
$('#CommodityTable').html(result);
},
});

return false;
});

</script>


<div id="CommodityTable">

@Html.WetStyleValidationSummary(true)
<table title="Current Commodities">
<tr>
<th>
Commodity Name
</th>
<th></th>
</tr>
@for (int i = 0; i < Model.CurrentCommodities.Count; i++)
{
<tr>
<td>
@Html.HiddenFor(model => Model.CurrentCommodities[i].Name)
@Html.DisplayFor(model => Model.CurrentCommodities[i].Name)
</td>
<td>
@Html.HiddenFor(model => Model.CurrentCommodities[i].Id)
<input name="currentCommoditiesToRemove" class="removeCheckbox" type="checkbox" value="@Model.CurrentCommodities[i].Id">
</td>
</tr>
}
</table>

<input id="remove-list" name="remove" type="button" value="Remove from Product" class="button button-accent ui-link" />

<table title="Searched Commodities" id="Searched">
<thead>
<tr>
<th>
Commodity Name
</th>
<th></th>
</tr>
@for (int j = 0; j < Model.AvailableCommodities.Count; j++)
{
<tr>
<td>
@Html.HiddenFor(model => Model.AvailableCommodities[j].Name)
@Html.DisplayFor(model => Model.AvailableCommodities[j].Name)
</td>
<td>
@Html.HiddenFor(model => Model.AvailableCommodities[j].Id)
<input name="availableCommoditiesToAdd" class="addCheckbox" type="checkbox" value="@Model.AvailableCommodities[j].Id" />
</td>
</tr>
}
</table>

<input id="add-list" name="add" type="button" value="Add To Product" class="button button-accent ui-link" />

</div>

这是 Controller 操作方法:

        [HttpPost]
public ActionResult UpdateCommodityList(string[] availableCommoditiesToAdd,
string[] currentCommoditiesToRemove, ProductLineCreateViewModel productModel)
{
}

由于某种原因,我的 POST 未到达 ActionMethod。为什么?

最佳答案

$.ajax 的 Url 参数是一个字符串,而不是一个对象,因此将该行更改为如下所示:

url: "@Url.Action("UpdateCommodityList", "ProductLine")",

关于c# - 使用 Jquery Ajax 将模型和 2 个复选框列表发布到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25429146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com