1
votes

After a user fills out a form, I need post the form contents back in JSON and get a chunk of HTML back for display. This seems like a good case for razor pages. The BrandTemplateInfo parameter on the OnPost handler is always null. I can't seem to get the BrandTemplateInfo to populate from the model binder. What am I missing here? This used to be easy with MVC controllers. What am I missing here? Help?

public class PayNowCardModel : PageModel
{
    public void OnGet()
    {
    }

    public IActionResult OnPost([FromBody] BrandTemplateInfo brandTemplateInfo)
    {
        return Page();
    }


    public BrandTemplateInfo BrandTemplateInfo { get; set; }
}

HTTP Request:

POST /terms/paynowcard HTTP/1.1
Host: xxxxxxxx
Content-Type: application/json
Cache-Control: no-cache

{
    "userForm": {
        "cardNumber": "4111111111111111",
        "paymentAmount": 123.33
    },
    "account": {
        "Creditor": {
            "Name": "big time creditor"
        },
        "accountId": "32432432432423"
    }
}
1

1 Answers

0
votes

You should make sure the BrandTemplateInfo object has the same name properties as the posted json property :

public class BrandTemplateInfo
{
    public userForm userForm { get; set; }

    public account account { get; set; }
}

public class account
{
    public Creditor Creditor { get; set; }
    public string accountId { get; set; }
}

public class Creditor
{
    public string Name { get; set; }
}

public class userForm
{
    public string cardNumber { get; set; }
    public float paymentAmount { get; set; }
}

So that model binding would work with [FromBody] attribute .