【问题标题】:How to send to controller with Ajax?如何使用 Ajax 发送到控制器?
【发布时间】:2021-12-05 05:17:36
【问题描述】:

我正在尝试使用 Ajax 查询将查询发送到控制器,但我做不到。我知道的不多,我是实习生。在做之前,我看了一下如何用ajax查询发送数据,但是没有用。如果您能帮助我,我将不胜感激。

我的 .aspx 代码:

<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 值。但数据不会进入控件。如果您能帮助我,我将不胜感激。

【问题讨论】:

    标签: asp.net ajax telerik asp.net-ajax


    【解决方案1】:

    您可以更改输入模型,而不是获取字符串,而是获取对象

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

    【讨论】:

    • 首先非常感谢你,我按照你说的换了控制器。没有改变。 Ajax 查询似乎有问题。如果 ajax 查询返回成功或不成功,我会打印一条消息。它发生不成功。错误状态:500 statusText:它给出的错误为“内部服务器错误”。
    • 哦,那你的操作有问题,可以设置断点调试一下
    • 我一个一个看。当涉及到 Ajax 查询中的 url 时,它会崩溃。当我查看错误中的响应文本时,我发现它给出了错误“未知的 web 方法 CustomActionOn”。 “参数名称:方​​法名称”。但我不认为网址有什么问题。
    • 这个问题有帮助吗? stackoverflow.com/questions/49268186/…
    • 我刚刚重新测试了它。通常ajax查询是正确的。问题来自这里。当您在控制器中将其作为公共无效时,它会给出错误。我认为给静态是强制性的。但是当我给静态时,我无法在控制器中提取数据。所以必须给出如下:“public static string CustomActionOn(ItemStockCodeModel itemStockCodeRequest)”
    猜你喜欢
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 2020-11-14
    • 2019-07-02
    相关资源
    最近更新 更多