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
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:
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.
I tested this code using net core mvc, and don’t know how it works on another versions:
and change your action to this: