skip to Main Content

I’m trying to post data to a controller in ASP.NET Core
I receive null in the controller.

I can’t get any data POSTed to an endpoint.

In the action

    [HttpPost]
    public ActionResult AddEarningg([FromBody] ProgramAddDTO program)
    {
        //_ProgramAppService.AddEarning(program);

        ViewData["tenantlist"] = ListItems();
        return View();
    }

In the ajax code:

<script src="~/Common/Scripts/select2.min.js"></script>
<script>
   

    $(document).ready(function () {
     $("#SubProductCodeId").select2();
    $("#ProductId").select2();
    $("#ProductCodeId").select2();
    $("#ProductCodeId").select2();
    $("#CountryId").select2();
    $("#MerchantId").select2();
    $("#ChannelId").select2();
    $("#SegmentId").select2();
    $("#BtnSubmit").click(function () {
        var formdata = {};
        
        formdata.institutionId = $("#InstitutionId").val();
        formdata.programName = $("#ProgramName").val();
        formdata.productId = $("#ProductId").val();
        formdata.productCodeId = $("#ProductCodeId").val();
         formdata.subProductId= $("#SubProductId").val();
         formdata.TransactionTypeId= $("#TransactionTypeId").val();
         formdata.ChannelId= $("#ChannelId").val();
         formdata.SubProductCodeId= $("#SubProductCodeId").val();
         formdata.SpendToEarn= $("#SpendToEarn").val();
         formdata.EquivalnetPoint= $("#EquivalnetPoint").val();
         formdata.CountryId= $("#CountryId").val();
         formdata.MCCId= $("#MCCId").val();
         formdata.MerchantId= $("#MerchantId").val();
         formdata.SegmentId= $("#SegmentId").val();
         formdata.CapsMin= $("#CapsMin").val();
         formdata.CapsMax= $("#CapsMax").val();
        formdata.PointsExpiry = $("#PointsExpiry").val();
        var t = JSON.stringify(formdata)
        $.ajax({
            url: '/app/Program/AddEarningg',
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ 'program': formdata }),
        
            success: function () {
                alert("test");
            }
        });
    });
       

    });     
</script>

in the DTO
ProgramAddDTO Class

public class ProgramAddDTO
{
    //public int? ProgramTypeId { get; set; }

    public int InstitutionId { get; set; }

    public string ProgramName { get; set; }

    public List<int?> ProductId { get; set; }
    public List<int?> ProductCodeId { get; set; }

    public List<int?> SubProductId { get; set; }
    public List<int?> SubProductCodeId { get; set; }

    public List<int?> CountryId { get; set; }

    public int? TransactionTypeId { get; set; }

   
    public int EquivalnetPoint { get; set; }
   
    public int PointValue { get; set; }
    public List<int?> ChannelId { get; set; }
    public List<int?> MCCId { get; set; }
    public List<int?> MerchantId { get; set; }
    public List<int?> SegmentId { get; set; }

    public decimal CapsMin { get; set; }

    public decimal CapsMax { get; set; }

    public int? PointsExpiry { get; set; }

    public int SpendToEarn { set; get; }
}

When I pass a complex JSON to the post method, then it always shows me null.

I’m using the select2 jquery library.

2

Answers


  1. Chosen as BEST ANSWER

    its working by me when parse value from form to int

        var pinstitutionId =parseInt( $("#InstitutionId").val());
            var pprogramName = $("#ProgramName").val();
            var pproductId = $("#ProductId").val();
            var pproductCodeId = $("#ProductCodeId").val();
            var psubProductId= $("#SubProductId").val();
            var ptransactionTypeId= parseInt($("#TransactionTypeId").val());
            var pchannelId= $("#ChannelId").val();
            var psubProductCodeId= $("#SubProductCodeId").val();
            var pspendToEarn= parseInt($("#SpendToEarn").val());
            var pequivalnetPoint= parseInt($("#EquivalnetPoint").val());
            var pcountryId= $("#CountryId").val();
            var pmCCId= $("#MCCId").val();
            var pmerchantId= $("#MerchantId").val();
            var psegmentId= $("#SegmentId").val();
            var pcapsMin= parseInt($("#CapsMin").val());
            var pcapsMax= parseInt($("#CapsMax").val());
            var ppointsExpiry =parseInt( $("#PointsExpiry").val()) ;
    

  2. First, don’t use Uppercase letters for your json properties. For example

    formdata.TransactionTypeId 
    

    should become

    formdata.transactionTypeId
    

    and so one.

    Because by default in .net core asp.net it looks for not capitalized json properties and it convert it to the correspondent property in C# that start with uppercase.

    And the request should works simply with:

    $.ajax({
            url: '/app/Program/AddEarningg',
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formdata) ,
    
            success: function () {
                alert("test");
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search