1

I have an Ajax call which has a list of parameters. One of them is a list of objects from the Model. I am having trouble trying to prepare the data to send to the Controller. Currently, it's like this:

const result = await $.ajax({
            type: "POST",
            url: "/PayrollAudit/SaveFundFindings",
            data: {
                'fundFindingsGridRows': DATA,
                'revisionDateExists': @Model.revisionDateExists.ToString().ToLower(),
                'rev': @Model.rev.ToString().ToLower(),
                'deficiency': @Model.deficiency,
                'principalAndInterest': @Model.principalAndInterest,
                'newAudit': @Model.newAudit.ToString().ToLower(),
                'auditStartDate': moment('@Model.auditStartDate').format('MM/DD/YYYY'),
                'billingEntityFindingsTitle': '@Model.billingEntityFindingsTitle',
                'delinquency20': @Model.delinquency20,
                'billingEntityNo': '@Model.billingEntityNo',
                'revisionSentToFundsDate': moment('@Model.revisionSentToFundsDate').format('MM/DD/YYYY'),
                'V3FundOptions': @Model.V3FundOptions
                }
        });

When this is run, I get the following error in the browser debug console:

Uncaught SyntaxError: Unexpected number (at PayrollAudit:2617:66)

Here is what the code looks like in the console:

enter image description here

Controller:

 public IActionResult SaveFundFindings(
        List<FundFindingsGridRow> fundFindingsGridRows,
        bool revisionDateExists,
        bool rev,
        bool settlement,
        decimal deficiency,
        decimal principalAndInterest,
        bool newAudit,
        DateTime auditStartDate,
        string billingEntityFindingsTitle,
        decimal delinquency20,
        string billingEntityNo,
        DateTime revisionSentToFundsDate,
        List<FundOption> V3FundOptions
        )
    {
        // Do things...
    }

FundOption Class:

public partial class FundOption
{
    public string ClientCode { get; set; }
    public string LvwColTitle { get; set; }
    public int ColOrder { get; set; }
    public string DbFieldName { get; set; }
}

I understand that using @Model is going to return the name of the collection and not the objects in the collection. So, what do I need to do here to properly send this data to the controller?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
RoastBeast
  • 1,059
  • 2
  • 22
  • 38

1 Answers1

0

Like @Heretic Monkey suggested in the comments, you need to serialize the list to JSON. Add @using System.Text.Json in your view and then use @Html.Raw(Json.Serialize(Model.V3FundOptions)):

const result = await $.ajax({
    type: "POST",
    url: "/PayrollAudit/SaveFundFindings",
    data: {
        'fundFindingsGridRows': DATA,
        'revisionDateExists': @Model.revisionDateExists.ToString().ToLower(),
        'rev': @Model.rev.ToString().ToLower(),
        'deficiency': @Model.deficiency,
        'principalAndInterest': @Model.principalAndInterest,
        'newAudit': @Model.newAudit.ToString().ToLower(),
        'auditStartDate': moment('@Model.auditStartDate').format('MM/DD/YYYY'),
        'billingEntityFindingsTitle': '@Model.billingEntityFindingsTitle',
        'delinquency20': @Model.delinquency20,
        'billingEntityNo': '@Model.billingEntityNo',
        'revisionSentToFundsDate': moment('@Model.revisionSentToFundsDate').format('MM/DD/YYYY'),
        'V3FundOptions': @Html.Raw(JsonSerializer.Serialize(Model.V3FundOptions))
    }
});
Dimitris Maragkos
  • 8,932
  • 2
  • 8
  • 26