在POST WEB API中,复杂类型中的一些长参数始终为零

时间:2014-04-17 12:55:52

标签: jquery asp.net asp.net-web-api

我的DTO是

public class create_invoice_request
{
    [DataMember]
    public long termID { get; set; }
    [DataMember]
    public long admissionBatchID { get; set; }
    [DataMember]
    public long programID { get; set; }
    [DataMember]
    public long admissionID { get; set; }
    [DataMember]
    public long bankAccountID { get; set; }
    [DataMember]
    public long companyID { get; set; }
    [DataMember]
    public long invoiceTypeID { get; set; }
    [DataMember]
    public decimal totalAmount { get; set; }
    [DataMember]
    public List<invoiceDiscountDetail> invoiceDisDetList { get; set; }
    [DataMember]
    public DateTime invoiceDate { get; set; }
    [DataMember]
    public DateTime invoiceDueDate { get; set; }
    [DataMember]
    public long createdBy { get; set; }
    [DataMember]
    public DateTime createdAt { get; private set; }
    [DataMember]
    public long? modifiedBy { get; set; }
    [DataMember]
    public DateTime? modifiedAt { get; private set; }
    [DataMember]
    public int[] stdIDs { get; set; }
    [DataMember]
    public List<feeSettingDetail> feeSettingDetailList { get; set; }
}

public class invoiceDiscountDetail
{
    [DataMember]
    public long invoiceDiscountDetailID { get; set; }
    [DataMember]
    public long invoiceID { get; set; }
    [DataMember]
    public long feeDiscountSettingDetailID { get; set; }
    [DataMember]
    public long feeTypeID { get; set; }
    [DataMember]
    public long feeDiscountTypeID { get; set; }
    [DataMember]
    public decimal discountAmount { get; set; }
}

public class feeSettingDetail
{
    [DataMember]
    public long feeSettingDetailID { get; set; }
    [DataMember]
    public decimal amount { get; set; }
}

JS是

var $id = [$('#ddlStudent').val()];
var feeSettingDetailList = [];

$('#feeDetailTable > tbody > tr').each(function (i, el) {
    var feeSettingDetail = {};

    feeSettingDetail["feeSettingDetailID"] = $(this).find("input[id^=txtAmount]").attr('id').substring(9);
    feeSettingDetail["amount"] = $(this).find("input[id^=txtAmount]").autoNumeric('get');

    feeSettingDetailList.push(feeSettingDetail);
});

var invoiceDisDetList = [];

$('#feeDiscountDetailTable > tbody > tr').each(function (i, el) {
    var invoiceDisDet = {};

    invoiceDisDet["feeDiscountSettingDetailID"] = $(this).find("input[id^=txtDiscountAmount]").attr('id').substring(17);
    invoiceDisDet["feeTypeID"] = $(this).find("select[id*=ddlDisFeeType]").val();
    invoiceDisDet["feeDiscountTypeID"] = $(this).find("select[id*=ddlFeeDiscountType]").val();
    invoiceDisDet["discountAmount"] = $(this).find("input[id^=txtDiscountAmount]").val();

    invoiceDisDetList.push(invoiceDisDet);
});

var source = {
    'termID': $('#ddlTerm').val(),
    'programID': $('#ddlProgram').val(),
    'admissionBatchID': $('#ddlBatch').val(),
    'ledgerBalance': $("#txtFeeBalance").val(),
    'bankAccountID': $('#ddlAccountNumber').val(),
    'invoiceTypeID': 1,
    'totalAmount': $("#lblTotal").html(),
    'invoiceDate': $("#txtInvoiceDate").val(),
    'invoiceDueDate': $("#txtDueDate").val(),
    'stdIDs': $id,
    'invoiceDisDetList': invoiceDisDetList,
    'feeSettingDetailList': feeSettingDetailList
};

我的控制器是

[HttpPost]
[ActionName("InsertInvoice")]
public HttpResponseMessage InsertInvoice(create_invoice_request req)
{

      //Controller body 
      //invoiceDiscountDetail some parameters are alwyas zero
      //  [DataMember]
      //  public long feeDiscountSettingDetailID { get; set; }
      //  [DataMember]
      //  public long feeTypeID { get; set; }
      //  [DataMember]
      //  public long feeDiscountTypeID { get; set; } 
}

1 个答案:

答案 0 :(得分:0)

我发现问题是本节中有一个额外的空格

invoiceDisDet["feeTypeID"] = $(this).find("select[id*=ddlDisFeeType]").val();
invoiceDisDet["feeDiscountTypeID"] = $(this).find("select[id*=ddlFeeDiscountType]").val();
invoiceDisDet["discountAmount"] = $(this).find("input[id^=txtDiscountAmount]").val();

所以我只使用$ .trim($(this).find(“select [id * = ddlDisFeeType]”)。val()); 并且问题已得到解决

我正在分享这个,所以它可以帮助其他人。