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?

2
  • 1
    Convert the list to JSON. Commented Sep 28, 2022 at 15:03
  • @HereticMonkey yes, it was the right track, thanks! Commented Sep 28, 2022 at 15:50

1 Answer 1

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))
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

This is exactly what the duplicate says.
That answer is for MVC 3 and uses Json.Encode. That doesn't work in ASP.NET Core.
@HereticMonkey provided a good place to look for the method.
@DimitrisMaragkos provided the exact code I needed. Thank you both.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.