skip to Main Content

I want to pass an array and an Id to the controller but every method I have tried gives me null/0 value.

Can someone please help me what’s missing in my code?

Here is my JS code:

addedParticulars = [{ FundRuleRid: fundRuleRid, FundRuleName: fundRuleName, Amount: amount }] //multiple values


function submitInvoice() {
    if (addedParticulars.length === 0)
        alert('Add any particular first')
    else {
        var jsonObject = JSON.stringify({ fundRules: addedParticulars, flatInvoiceRid: flat_invoice_Rid });
        console.log(jsonObject);
         $.ajax({
            type: "POST",
            url: "/FlatInvoice/ProcessInvoiceAmendment",
            contentType: "application/json; charset=utf-8",
            dataType: 'JSON', 
            data: jsonObject,
            traditional: true
        });
    }
}

This is my Action Method

public JsonResult ProcessInvoiceAmendment(List<FundRuleModel> fundRules, Guid flatIinvoiceRid)
{
     return Json(new { status = _requestStatus, message = _responseMessage });
}

and this is my .NET class model

 public class FundRuleModel : BaseModel
    {
        public bool IsActive { get; set; }
        public Guid FundRuleRid { get; set; }

        public string FundRuleName { get; set; }

        public int FundCycleRid { get; set; }
        public string FundCycleName { get; set; }

        public int FundRuleTypeRid { get; set; }
        public string FundRuleTypeName { get; set; }

        public int FundCalculationTypeRid { get; set; }
        public string FundCalculationTypeName { get; set; }

        public int FlatCalculationFilterRid { get; set; }
        public string FlatCalculationFilterName { get; set; }

        public decimal Amount { get; set; }

        public DateTime ApplicableDate { get; set; }
        public DateTime? EndDate { get; set; }

        public string Narration { get; set; }

        public bool IsIncludedInRegularInvoice { get; set; } = true;

        //Used in FlatInvoiceLogic
        public int Category { get; set; }
    }

I’m getting all the values in console but nothing is pass to the controller.

2

Answers


  1. Chosen as BEST ANSWER

    After some trial and error I found the solutions. Thank to @Sergey and @Hopeless for the help. Code is working fine without JSON.

    I just changed the object format and it's working now; see the changes below:

     var jsonObject = {
                fundRules: addedParticulars ,
                flatIinvoiceRid: flat_invoice_Rid
            };
            console.log(jsonObject);
            $.ajax({
                type: "POST",
                url: "/FlatInvoice/ProcessInvoiceAmendment",
                data: jsonObject
            });
    

    When I added content-type :'application/json' and dataType:'JSON' in ajax it's getting null values on controller (Even after stringifying the object) but when I tried by removing JSON manipulation. It's working fine.


  2. I tested this code using net core mvc, and don’t know how it works on another versions:

    addedParticulars = [{ FundRuleRid: fundRuleRid, 
    FundRuleName: fundRuleName, Amount: amount }];
    
    $.ajax({
                type: "POST",
                url: "/FlatInvoice/ProcessInvoiceAmendment/"+flat_invoice_Rid,
                contentType: "application/json; charset=utf-8", 
                dataType: 'json',
                data: JSON.Stringify(addedParticulars),
                success: function (result) {
                   ....
                },
                error: function (xhr, exception) {
                   .....
                }
               });
    

    and change your action to this:

    [Route("~/FlatInvoice/ProcessInvoiceAmendment/{flatIinvoiceRid}")]````
    public JsonResult ProcessInvoiceAmendment([FromBody] IEnumerable<FundRuleModel> fundRules, string flatIinvoiceRid)
    {
    Guid  gflatIinvoiceRid= new Guid(flatIinvoiceRid);
    ......
         return Json(new { status = _requestStatus, message = _responseMessage });
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search