1

I just converted json to c# class using json2csharp.com. Then trying to pass that json from view to my controller but problem is only "create_time" object value is garbing successfully no any other value is able to grab. Also please check my debug picture to get better idea. What mistake it can be?

Picture

Json i am passing from view:

{"id":"WH-0G571461Y8752214W-8RU19841BY148894W","create_time":"2017-02-27T23:14:14Z","resource_type":"invoices","event_type":"INVOICING.INVOICE.CREATED","summary":"An invoice has been created","resource":{"id":"INV2-3RK5-HZV6-35UP-3LLU","number":"8035","template_id":"TEMP-6MT19746YC041742U","status":"DRAFT","merchant_info":{"email":"[email protected]","first_name":"Dennis","last_name":"Doctor","business_name":"Medical Professionals, LLC","phone":{"country_code":"001","national_number":"5032141716"},"address":{"line1":"1234 Main St.","city":"Portland","state":"OR","postal_code":"97217","country_code":"US"}},"billing_info":[{"email":"[email protected]"}],"shipping_info":{"first_name":"Sally","last_name":"Patient","business_name":"Not applicable","phone":{"country_code":"001","national_number":"5039871234"},"address":{"line1":"1234 Broad St.","city":"Portland","state":"OR","postal_code":"97216","country_code":"US"}},"items":[{"name":"Sutures","quantity":100,"unit_price":{"currency":"USD","value":"5.00"}}],"invoice_date":"2017-02-27 PDT","payment_term":{"term_type":"NET_45","due_date":"2017-04-13 PDT"},"tax_calculated_after_discount":false,"tax_inclusive":false,"note":"Medical Invoice 27 Feb, 2017","total_amount":{"currency":"USD","value":"500.00"},"metadata":{"created_date":"2017-02-27 14:42:03 PDT"},"allow_tip":false,"links":[{"rel":"self","href":"https://api.paypal.com/v1/invoicing/invoices/INV2-3RK5-HZV6-35UP-3LLU","method":"GET"},{"rel":"send","href":"https://api.paypal.com/v1/invoicing/invoices/INV2-3RK5-HZV6-35UP-3LLU/send","method":"POST"},{"rel":"update","href":"https://api.paypal.com/v1/invoicing/invoices/INV2-3RK5-HZV6-35UP-3LLU/update","method":"PUT"},{"rel":"delete","href":"https://api.paypal.com/v1/invoicing/invoices/INV2-3RK5-HZV6-35UP-3LLU","method":"DELETE"}]},"links":[{"href":"https://api.paypal.com/v1/notifications/webhooks-events/WH-0G571461Y8752214W-8RU19841BY148894W","rel":"self","method":"GET","encType":"application/json"},{"href":"https://api.paypal.com/v1/notifications/webhooks-events/WH-0G571461Y8752214W-8RU19841BY148894W/resend","rel":"resend","method":"POST","encType":"application/json"}],"event_version":"1.0"}

Custom ViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication2.WebhookModels.paypal
{
    public class Phone
    {
        public string country_code { get; set; }
        public string national_number { get; set; }
    }

    public class Address
    {
        public string line1 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string postal_code { get; set; }
        public string country_code { get; set; }
    }

    public class MerchantInfo
    {
        public string email { get; set; }
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string business_name { get; set; }
        public Phone phone { get; set; }
        public Address address { get; set; }
    }

    public class BillingInfo
    {
        public string email { get; set; }
    }

    public class Phone2
    {
        public string country_code { get; set; }
        public string national_number { get; set; }
    }

    public class Address2
    {
        public string line1 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string postal_code { get; set; }
        public string country_code { get; set; }
    }

    public class ShippingInfo
    {
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string business_name { get; set; }
        public Phone2 phone { get; set; }
        public Address2 address { get; set; }
    }

    public class UnitPrice
    {
        public string currency { get; set; }
        public string value { get; set; }
    }

    public class Item
    {
        public string name { get; set; }
        public int quantity { get; set; }
        public UnitPrice unit_price { get; set; }
    }

    public class PaymentTerm
    {
        public string term_type { get; set; }
        public string due_date { get; set; }
    }

    public class TotalAmount
    {
        public string currency { get; set; }
        public string value { get; set; }
    }

    public class Metadata
    {
        public string created_date { get; set; }
    }

    public class Link
    {
        public string rel { get; set; }
        public string href { get; set; }
        public string method { get; set; }
    }

    public class Resource
    {
        public string id { get; set; }
        public string number { get; set; }
        public string template_id { get; set; }
        public string status { get; set; }
        public MerchantInfo merchant_info { get; set; }
        public List<BillingInfo> billing_info { get; set; }
        public ShippingInfo shipping_info { get; set; }
        public List<Item> items { get; set; }
        public string invoice_date { get; set; }
        public PaymentTerm payment_term { get; set; }
        public bool tax_calculated_after_discount { get; set; }
        public bool tax_inclusive { get; set; }
        public string note { get; set; }
        public TotalAmount total_amount { get; set; }
        public Metadata metadata { get; set; }
        public bool allow_tip { get; set; }
        public List<Link> links { get; set; }
    }

    public class Link2
    {
        public string href { get; set; }
        public string rel { get; set; }
        public string method { get; set; }
        public string encType { get; set; }
    }

    public class RootObject
    {
        public string id { get; set; }
        public DateTime create_time { get; set; }
        public string resource_type { get; set; }
        public string event_type { get; set; }
        public string summary { get; set; }
        public Resource resource { get; set; }
        public List<Link2> links { get; set; }
        public string event_version { get; set; }
    }
}

Controller:

[HttpPost]
        public ActionResult InvoicePaid(RootObject rootObject)
        {
            using (var ctx = new db_someEntities())
            {

            }
            return Json("ok");
        }
2
  • You have shown what is being sent but not how it is being sent form the view. is it being submitted via form or ajax. Commented Dec 26, 2017 at 13:19
  • $.post(hookUrl, { testJson: testJson }) .done(function (data) { console.log(data); }); yes using jquery post method @Nkosi Commented Dec 26, 2017 at 13:39

3 Answers 3

2

I'll assume you're on dotnet core (maybe on a migrated project?) because the code you're posting doesn't have any issues on previous versions of asp.net mvc.

If that's the case, it turns out that on dotnet core the model binding behaviour has changed a little compared to previous versions.

If you want the solution quickly just decorate the parameter of your POST action with [FromBody] attribute. It should work.

[HttpPost]
public ActionResult InvoicePaid([FromBody]RootObject rootObject)
{
    using (var ctx = new db_someEntities())
    {
    }
    return Json("ok");
}

The story is a little longer though, and very interesting by the way. If you want the details, I've not found any better resource than this post from Andrew Lock.

UPDATE I can confirm that if you add the [FromBody] on the action, (I've tested using a bare new asp.net core 2. web app project) it binds the model correctly.

Json model binds successfully

Please let me know if you want me to provide the sources I've used so you can troubleshot from there.

Hope this helps!

Sign up to request clarification or add additional context in comments.

8 Comments

No answer not works. The "create_time" object value only works
@Kevinhimch, are you using dotnet core?
Yes sure. using System.Web.Http; also [System.Web.Http.HttpPost] used but still not works!
Weird then because I tested the code before answering and after putting the [FromBody] I was able to receive the model successfully.
@Kevinhimch, please see the update! If you need more help just let me know.
|
2

Most probably the data being sent it not formatted properly

From comments it was indicated that the data was being sent like

$.post(hookUrl, { testJson: testJson }).done(function (data) { console.log(data); });

consider formatting the data and including the content type

var data = JSON.stringify(testJson);  //replace content with your JSObject
$.post(hookUrl, data, null, "application/json")
 .done(function (data) { console.log(data); });

Or using the longer syntax

var data = JSON.stringify(testJson); //replace content with your JSObject
$.ajax({
  type: "POST",
  url: hookUrl,
  data: data,
  dataType: "application/json",
  success: function (data) { console.log(data); }
});

2 Comments

No answer not works. The "create_time" object value only works
@Kevinhimch the create_time is the default value, so it is also not working. can you show the raw request being sent to the server. the url is correct, how and what data you send is the problem.
0

You need to use [FromBody], so MVC knows where to look for the data.

[HttpPost]
public ActionResult InvoicePaid([FromBody]RootObject rootObject)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.