如何发送到控制器与Ajax?



我试图用Ajax查询将查询发送到控制器,但我不能。我知道的不多,我是实习生。在此之前,我研究了如何使用ajax查询发送数据,但它不起作用。如果你能帮助我,我将不胜感激。

my .aspx code:

<script type="text/javascript">
function customActionOn(itemStockCode) {
$.ajax({
url: '/SAPManagement/SAPUpdate.aspx/CustomActionOn',
dataType: 'json',
type: 'POST',
data: "{'itemStockCode': '"+itemStockCode+"'}",
contentType: 'application/json; charset=utf-8'
});
}
function customActionOff(itemStockCode) {
$.ajax({
url: '/SAPManagement/SAPUpdate.aspx/CustomActionOff',
dataType: 'json',
type: 'POST',
data: "{'itemStockCode': '" + itemStockCode + "'}",
contentType: 'application/json; charset=utf-8'
});
}
</script>

<telerik:GridTemplateColumn UniqueName="GetDeatils" ShowFilterIcon="false" AllowFiltering="false" HeaderText="Open and Close The Product On The Web" FilterControlWidth="10px" AutoPostBackOnFilter="false">
<ItemTemplate>
<%--<asp:Button CommandName="GetDeatils" ID="GetDeatils2" OnClientClick='<%# "javascript:openwin("+Eval("ItemStockCode").ToString()+")" %>'
runat="server" Text="Detay"></asp:Button>--%>
<asp:Button ID="Button3" runat="server" CssClass="btn btn-block green" Text="Open" OnClientClick='<%# "customActionOn("+Eval("ItemStockCode").ToString()+")" %>'></asp:Button>
<asp:Button ID="Button1" runat="server" CssClass="btn btn-block red" Text="Close" OnClientClick='<%# "customActionOff("+Eval("ItemStockCode").ToString()+")" %>'></asp:Button>
</ItemTemplate>
</telerik:GridTemplateColumn>

.aspx.cs代码:

[System.Web.Services.WebMethod]
public void CustomActionOn(string itemStockCode)
{
products = new LogicRules.B2B.BL_Product().GetAllProductsForSAPUpdate().OrderBy(x => x.ItemName).ToList();
var selectedProduct = products.Where(x => x.ItemStockCode == itemStockCode).FirstOrDefault();
selectedProduct.PublishOnB2B = true;
new Core.Log.ArkLogs().WriteProductUpdateLog(JsonConvert.SerializeObject(selectedProduct), User.Identity.GetUserId<int>(), "Product", "ISBN", selectedProduct.ItemStockCode);
var retValues = new SapFunction.ProductFunction().updateProduct(selectedProduct);
}

[System.Web.Services.WebMethod]
public void CustomActionOff(string itemStockCode)
{
products = new LogicRules.B2B.BL_Product().GetAllProductsForSAPUpdate().OrderBy(x => x.ItemName).ToList();
var selectedProduct = products.Where(x => x.ItemStockCode == itemStockCode).FirstOrDefault();
selectedProduct.PublishOnB2B = false;
new Core.Log.ArkLogs().WriteProductUpdateLog(JsonConvert.SerializeObject(selectedProduct), User.Identity.GetUserId<int>(), "Product", "ISBN", selectedProduct.ItemStockCode);
var retValues = new SapFunction.ProductFunction().updateProduct(selectedProduct);
}

我不能用这种方式向控制器发送数据。我得到itemstockcode值正确。但是这些数据并没有进入控制组。如果你能帮助我,我将不胜感激。

你可以改变你的输入模型,而不是获取字符串,获取一个对象

[System.Web.Services.WebMethod]
public void CustomActionOff(ItemStockCodeModel itemStockCodeRequest)
{
products = new LogicRules.B2B.BL_Product().GetAllProductsForSAPUpdate().OrderBy(x => x.ItemName).ToList();
var selectedProduct = products.Where(x => x.ItemStockCode == itemStockCodeRequest.itemStockCode).FirstOrDefault();
selectedProduct.PublishOnB2B = false;
new Core.Log.ArkLogs().WriteProductUpdateLog(JsonConvert.SerializeObject(selectedProduct), User.Identity.GetUserId<int>(), "Product", "ISBN", selectedProduct.ItemStockCode);
var retValues = new SapFunction.ProductFunction().updateProduct(selectedProduct);
}

并在命名空间

中定义一个模型
public class ItemStockCodeModel
{
public string ItemStockCode{set;get;}
}

最新更新