2012-08-15 58 views
0

我有webmethods作为“插入”和“更新”,我有我的记录的C#类为“DataParam”...我通过使用AJAX调用它。对于更新方法,没有问题,我已通过Ajax调用发送Class并进行更新,但是对于Insert,我有错误500. 我无法克服它。从Ajax发送C#类,错误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"} 
+0

什么是实际错误? – SLaks 2012-08-15 14:13:51

+0

什么是具体的500错误? 500是服务器错误,因此请检查服务器的事件日志以查看异常情况。或者使用Fiddler或Firebug来查看客户端的响应。 – 2012-08-15 14:14:02

+0

我编辑关于错误消息的问题,我认为它是关于类和变体类型和值。 – efirat 2012-08-15 14:21:04

回答

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

检查:数据= JSON.stringify({PARAM:记录});

+0

我已经尝试了很多不同的方式,问题依然存在。不过,我对此有所了解,我认为“记录”中的项目顺序会导致错误。当我阅读关于这个问题的文章时,我已经明白了这一点。我也用另一种方式克服了它,更新方法运行,首先它创建一个新的空记录然后更新它,用户看到保存一条新记录。如果用户不保存,则创建的新记录将被删除。它现在运行,当我有时间时,我会尝试关于“记录”中项目的顺序。 谢谢。 – efirat 2012-08-24 12:21:59