从Ajax发送C#类,错误500

时间:2012-08-15 14:11:24

标签: c# jquery ajax webmethod

我有webmethods作为“插入”和“更新”,我的记录为C#Class为“DataParam”......我是通过使用AJAX调用来实现的。对于更新方法,没有问题,我已通过Ajax调用发送Class并进行更新,但是对于Insert我有错误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 Call

        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"}

1 个答案:

答案 0 :(得分: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});