0

我有 webmethods 作为“插入”和“更新”,我的记录有 C# 类作为“DataParam”......我通过使用 AJAX 调用来做到这一点。对于更新方法,没有问题,我通过 Ajax 调用发送类并进行更新,但是插入我有错误 500。我无法克服它。

这是我的课:

    public class DataParam
{

    public string ATC1 { get; set; }
    public string ATC2 { get; set; }
    public string ATC3 { get; set; }
    public string ATC4 { get; set; }
    public string CurentlyProjectMark { get; set; }
    public int Dosage { get; set; }
    public string Dosage_Title { get; set; }
    public string FirstMark { get; set; }
    public int Form { get; set; }
    public string Form_Title { get; set; }
    public long Id { get; set; }
    public Guid Key { get; set; }
    public string LicensedTradeMark { get; set; }
    public long Molecule { get; set; }
    public string Molecule_Title { get; set; }
    public string ProductName { get; set; }
    public int ProjectCode { get; set; }
    public string ProjectCode_Title { get; set; }
    public DateTime RD_DataProtectionEndDate { get; set; }
    public string RD_Description { get; set; }
    public int RD_Document_Author { get; set; }
    public DateTime RD_Document_CheckDate { get; set; }
    public string RD_Document_CheckDescription { get; set; }
    public DateTime RD_Document_ComingDate { get; set; }
    public string RD_Document_ComingDescription { get; set; }
    public int RD_Document_Controller { get; set; }
    public DateTime RD_Document_DoneDate { get; set; }
    public string RD_Document_DoneDescription { get; set; }
    public DateTime RD_Document_RealesedDate { get; set; }
    public string RD_Document_RealesedDescription { get; set; }
    public int RD_Document_Status { get; set; }
    public DateTime RD_Document_TargetDate { get; set; }
    public string RD_Document_TargetDescription { get; set; }
    public int RD_ProjectedFirm { get; set; }
    public string RD_ProjectedName { get; set; }
    public int RD_Responsible { get; set; }
    public int RD_Status { get; set; }
    public string Title { get; set; }
    public string TradeMark { get; set; }
}

这是方法

        [WebMethod(EnableSession = true)]
    public static object Update(DataParam param = null)
    {
        try
        {
            CPI.RD.Product product = new CPI.RD.Product();
            object objProduct = (CPI.RD.Product)product;
            product = (CPI.RD.Product)CPI.RD.Helper.FillBussinesObject(param, ref objProduct);

            CPI.RD.ProductManagement.Update(product);

            return true;
        }
        catch (Exception ex)
        {
            return new { Result = "ERROR", Message = ex.Message };
        }
    }

    [WebMethod(EnableSession = true)]
    public static object Insert2(DataParam param = null)
    {
        try
        {

            CPI.RD.Product product = new CPI.RD.Product();
            object objProduct = (CPI.RD.Product)product;
            product = (CPI.RD.Product)CPI.RD.Helper.FillBussinesObject(param, ref objProduct);

            CPI.RD.ProductManagement.Insert(product);

            return true;
        }
        catch (Exception ex)
        {
            return new { Result = "ERROR", Message = ex.Message };
        }
    }

这是 AJAX 调用

        var param = cpiGrid.getRecordForm(record);

        // Check if it is new record
        if ($(div).attr("data-new") == "true") {

            url += "Insert2";

            record = cpiGrid.getRecordForm(record);

            data = JSON.stringify(record);
            console.log("send to add: ", record, " data: ", data);

            cpiGrid.getJSON(url, data, function () {
                cpiGrid.listRecords();
                $(".close-button").trigger("click");
            });
        }

详细错误信息:

{"Message":"Invalid web service call, missing value for parameter: \u0027param\u0027.","StackTrace":"   at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
4

1 回答 1

1

您需要在 JSON.stringify 部分指定参数名称,如下所示:

    // Check if it is new record 
    if ($(div).attr("data-new") == "true") { 

        url += "Insert2"; 

        record = cpiGrid.getRecordForm(record); 

        data = JSON.stringify({param: record}); 
        console.log("send to add: ", record, " data: ", data); 

        cpiGrid.getJSON(url, data, function () { 
            cpiGrid.listRecords(); 
            $(".close-button").trigger("click"); 
        }); 
    } 

检查:data = JSON.stringify({param:record});

于 2012-08-17T06:20:48.543 回答