0

I've a js object like bellow:

$scope.docPropIdentityModel = {
        Owner: {OwnerID:"", OwnerName: ""},
        };

I want to send this object to my mvc controller through ajax call. Let say the controller is like:

controller(test_class model)
{
}

and the model is like:

test_class
{
  public string Owner{get;set;};
}    

I'm getting null in my controller. How can I map the js object value to my model?

2 Answers 2

2

Yur json object includes two different classes. The top level class which holds a second object (Owner). You should have two classes:

public class TestClass {
    public Owner owner;

}
public class Owner {
    public String ownerId;
    public String ownerName;
}

Your JSON object with proper naming conventions:

{
"owner":{"ownerId":"yourID", "ownerName":"yourOwnerName"}
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way to map just the OwnerID to my TestClass model?
Your TestClass should be like ´public class TestClass { public String ownerId;}' your JSON object like ´{"ownerId":"yourID"}´. But then you loose information about owner name.
0

If you are using AngularJS, use $http.get or $http.post.

Controller:

[HttpPost]
public ReturnType Foo(YourModelClass modelClass)
{
}

JavaScript:

$http({
  url: url,
  method: 'POST',                    
  data: $scope.docPropIdentityModel                   
 })
   .success(function (data) {
        alert("OK");
   })
   .error(function (data) {
        alert("error");
   });

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.