1

I have following controller:

    [HttpPost]
    public JsonResult SaveCompensationComponent(int agencyId, CompensationComponentAllData compensationComponent)
    {
        int? id = _dmsCompensation.SaveCompensationComponent(agencyId, compensationComponent);
        return Json(id, JsonRequestBehavior.AllowGet);
    }

Definition of the CompensationComponentAllData class:

public class CompensationComponentAllData
{
    some properties

    ...

    public CompensationObject CompensationElement { get; set; }

    public CompensationObject CompensationBasis { get; set; }

    ...        

    some properties

    ...
}

CompensationObject class:

public class CompensationObject
{
    some properties

    ...

    public ActivityData ByActivity { get; set; }

    ...        

    some properties

    ...
}

ActivityData class:

public class ActivityData
{
    some properties

    ...

    public List<int> GoalCategories { get; set; }

    public List<int> Policies { get; set; }

    ...        

    some properties

    ...


    public ActivityData() { }

    public ActivityData(CompensationObjectByActivity record) {

        ...
    }
}

In javascript I create appropriate objects and send that through ajax (contentType: 'application/json' and JSON.stringify used before sending data). Everything is correctly sent to server because I executed following code in the controller:

HttpContext.Request.InputStream.Seek(0, SeekOrigin.Begin);
var jsonStringData = new StreamReader(HttpContext.Request.InputStream).ReadToEnd();

And jsonStringData had correct value:

"{\"agencyId\":\"332\",\"compensationComponent\":{\"Id\":431,\"CompensationComponentInfoId\":509,\"AgencyJobPositionId\":\"306\",\"Name\":\"ggggggg44\",\"Description\":\"fdssssssss\",\"CompensationComponentImpactLevelId\":\"1\",\"CompensationProductionTypeId\":\"1\",\"CompensationFrequency\":{\"Id\":\"1\"},\"CompensationDateTypeId\":\"3\",\"EffectiveDate\":\"06/10/2015\",\"BasisDifferentThanElement\":false,\"CompensationElement\":{\"ObjectTypeId\":\"3\",\"ByActivity\":{\"CompensationAttributeId\":\"21\",\"UnitsId\":\"3\",\"PoliciesLevel\":\"Individual Company/Policy\",\"Policies\":[\"572\",\"139\",\"138\"],\"VerificationLevelId\":\"1\"}},\"CompensationBasis\":{\"ObjectTypeId\":\"3\",\"ByActivity\":{\"CompensationAttributeId\":\"21\",\"UnitsId\":\"3\",\"PoliciesLevel\":\"Individual Company/Policy\",\"Policies\":[\"572\",\"139\",\"138\"],\"VerificationLevelId\":\"1\"}},\"CompensationStructureId\":\"2\",\"CompensationRateId\":\"1\",\"FixedValue\":\"10.00\",\"UseChargeback\":false}}"

Now problem is that after binding compensationComponent.CompensationElement.ByActivity.Policies has a null value even though it should be a list with 3 elements.

What makes me even more confused is that in the same time compensationComponent.CompensationBasis.ByActivity.Policies is bound correctly. Also compensationComponent.CompensationElement.ByActivity.GoalCategories is bound correctly too.

EDIT:

This is my ajax call:

$.ajax({
    type: type,
    url: url,
    data: JSON.stringify(data),
    success: callback,
    error: function (xhr, ajaxOptions, thrownError) {
        ...
    },
    contentType: 'application/json',
    cache: cache
});

If I remove JSON.stringify and try something as suggested in this post I get error in binding so 500 is just returned.

Please help.

7
  • Have you tried changing the JSON format from "Policies":["572","139","138"] to "Policies":[572,139,138] so it is posted as a number instead of as strings? Commented Jun 8, 2015 at 16:53
  • Do you use custom model binder for this object? Commented Jun 8, 2015 at 17:25
  • @br4d when do you mean? I have list of ints in js but stringify converst everything to string. Also if that would be a problem then compensationComponent.CompensationBasis.ByActivity.Policies wouldn't be bound too. Commented Jun 8, 2015 at 17:32
  • @Mariusz I don't use custom model binder. Commented Jun 8, 2015 at 17:32
  • Try to remove CompensationBasis from JSON and check if second object will be bound correctly maybe it will give us some hint. Commented Jun 8, 2015 at 17:35

1 Answer 1

0

I think that's a little bit strange that you want to receive part of the JSON as method parameters. I think you need to introduce a class for root object:

public class CompensationModel
{
    public int AgencyId { get; set; }
    public CompensationComponentAllData CompensationComponent { get; set; }
}

And your controller method will take this class as a parameter:

[HttpPost]
public JsonResult SaveCompensationComponent(CompensationModel model)

I suggest you to use services like json2csharp, let's computer does the boring work.

And few comments about JSON that you send. JSON.stringify doesn't change types:

JSON.stringify({a: [1, 2, 3]}) // -> "{"a":[1,2,3]}"
JSON.stringify({a: ["1", "2", "3"]}) // -> "{"a":["1","2","3"]}"

So if you get strings in your JSON that menas that javascript values are of type string. In C# models you expect to have int, so you might consider to convert data in javascript, though MVC is smart enough to convert these values.

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

3 Comments

I tried like that and now it's not even setting the policies for CompensationBasis. When I put agencyId to root model it was acting same like before root model.
For now I made a workaround by putting policies list to one place higher in hierarchy and it works but I would really like to discover why is this happening,
I updated my answer. Check CompensationModel class and SaveCompensationComponent method parameters.

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.