Can't bind view model properly

seeker

I want to create client side logging infrastructure using log4javascript. All logs are sent to server. That part works well. I have created WebApi controller to write those log messages on server. The request is the following:

Request Headers

I have created the following view models:

 public class LogEntryViewModel
{
    public string logger { get; set; }
    public int timestamp { get; set; }
    public string level { get; set; }
    public string url { get; set; }
    public string[] message { get; set; }
}

public class LoggerViewModel
{
  public LogEntryViewModel data { get; set; }
  public string layout { get; set; }
}

But the problem is that all data in LogEntryViewModel is default. Even when I change public LogEntryViewModel data to public JObject data that results in empty data object. I tried to parse Json that request contain and it is correct. What the problem might be here?

EDIT:

When I have changed Content-Type to application/json;charset=UTF-8 The request looks in the following way:

Request #2

And then I have changed Post method on controller:

// POST api/loggerservice
    public void Post([FromBody]LogEntryViewModel log)
    {
        //BL
    }

However, still the same effect.

Andrew

In Web API, the media type determines how Web API serializes and deserializes the HTTP message body. There is built-in support for XML, JSON, and form-urlencoded data, and you can support additional media types by writing a media formatter.

I think that, based on your content-type (x-www-form-urlencoded), Web Api expects something like: logger=AjaxLogger&timestamp=1375705087456... so it can use the serializer for form-urlencoded data.

Since you're sending JSON to the server I would suggest to set the Content-Type to application/json and use JSON.stringify so it can use the JSON serializer for model binding:

data: JSON.stringify({'logger':'AjaxLogger', 'timestamp': '1234568789'})

You might want to remove the layout property as well now.

You don't need the [FromBody]. This blogpost helped me to understand the [FromBody] attribute

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't get my knockout to bind properly

From Dev

can't get model to properly validate

From Dev

can't get model to properly validate

From Dev

Can't bind data to view angularjs

From Dev

Backbone bind Model to View

From Dev

View can't access Model MVC

From Dev

Bind Model to a View asynchronously in Backbone

From Dev

Bind selected item to view model

From Dev

Can't bind a class from VM to View in WPF

From Dev

How to bind Func<T,T,T> to dependency property in XAML from view model?

From Dev

ng-model doesn't bind properly in directive template's select

From Dev

ng-model doesn't bind properly in directive template's select

From Dev

Can't align image view properly in relative layout

From Dev

Unable to model bind a collection within a view model

From Dev

Knockout.js - Doesn't seem to bind properly, can't figure out why

From Dev

In Aurelia, can I bind a function from my containing view-model to be called by my custom element?

From Dev

Laravel 4 Model Relationship not working properly. Can't access related column from Model

From Dev

Laravel 4 Model Relationship not working properly. Can't access related column from Model

From Dev

Asp.net MVC model won't bind list property to view

From Dev

Can't display data in view from model in Laravel

From Dev

Can't access model attributes from Backbone View

From Dev

Can't use updated model with association in the same view

From Dev

Can't get the view updated, even though the model is already updated

From Dev

Can't get Angularjs to update view when model updates

From Dev

Can't create Model by View and Partialview updated using ajax

From Dev

Can we bind the ManagedObject to View?

From Dev

Can't bind to a property

From Dev

Can't bind array

From Dev

Can't bind to 'ng-model' since it isn't a known native property

Related Related

  1. 1

    Can't get my knockout to bind properly

  2. 2

    can't get model to properly validate

  3. 3

    can't get model to properly validate

  4. 4

    Can't bind data to view angularjs

  5. 5

    Backbone bind Model to View

  6. 6

    View can't access Model MVC

  7. 7

    Bind Model to a View asynchronously in Backbone

  8. 8

    Bind selected item to view model

  9. 9

    Can't bind a class from VM to View in WPF

  10. 10

    How to bind Func<T,T,T> to dependency property in XAML from view model?

  11. 11

    ng-model doesn't bind properly in directive template's select

  12. 12

    ng-model doesn't bind properly in directive template's select

  13. 13

    Can't align image view properly in relative layout

  14. 14

    Unable to model bind a collection within a view model

  15. 15

    Knockout.js - Doesn't seem to bind properly, can't figure out why

  16. 16

    In Aurelia, can I bind a function from my containing view-model to be called by my custom element?

  17. 17

    Laravel 4 Model Relationship not working properly. Can't access related column from Model

  18. 18

    Laravel 4 Model Relationship not working properly. Can't access related column from Model

  19. 19

    Asp.net MVC model won't bind list property to view

  20. 20

    Can't display data in view from model in Laravel

  21. 21

    Can't access model attributes from Backbone View

  22. 22

    Can't use updated model with association in the same view

  23. 23

    Can't get the view updated, even though the model is already updated

  24. 24

    Can't get Angularjs to update view when model updates

  25. 25

    Can't create Model by View and Partialview updated using ajax

  26. 26

    Can we bind the ManagedObject to View?

  27. 27

    Can't bind to a property

  28. 28

    Can't bind array

  29. 29

    Can't bind to 'ng-model' since it isn't a known native property

HotTag

Archive