Pooria.Shariatzadeh
Pooria.Shariatzadeh

Reputation: 301

pass data from angularjs controller to C# MVC CTRL

this is simple but it dosent work

in angularJS part i have this code :

   var data={"ID":1,"Key":"********"}
            $http.post("/Home/DeleteListItem", data)
                .success(function (data) {
                    alert(JSON.parse(data));
                }).error(function (response) {
                    alert(response);
                });

and C# Part is like this

 [HttpPost]
public JsonResult DeleteListItem(Entity entity)

{

kishAppEntities db = new kishAppEntities();
Stream req = Request.InputStream;
db.Configuration.ProxyCreationEnabled = false;
var data = //some delete query

return new JsonResult()
{
    Data = data,
    JsonRequestBehavior = JsonRequestBehavior.AllowGet

};

}


public  class Entity
{
    int ID { set; get; }
    string Key { set; get; }

}

there is no data in entity i dont have clue aboute it???? enter image description here

i use this post method as second approach but still dosent work

     var entity = { "ID": 1, "Key": "********" }
        $http({
            url: "./general/DeleteListItem",
            method: "POST",
            data: JSON.stringify({
                entity: entity
        }),
        headers: {
            'Content-Type': 'application/json'
        }
        }).success(function(data, status, headers, config) {           
        }).error(function(data, status, headers, config) {
        });

Upvotes: 0

Views: 1093

Answers (3)

a-man
a-man

Reputation: 707

Your ID and Key properties are not accessible. Put before your properties public access modifier like this:

public  class Entity
{
    public int ID { set; get; }
    public string Key { set; get; }

}

Upvotes: 1

Vasim
Vasim

Reputation: 166

Try using jquery $.param(data). You can also try angular $httpParamSerializer https://code.angularjs.org/1.4.0/docs/api/ng/service/$httpParamSerializer

Upvotes: 0

Kalyan
Kalyan

Reputation: 1200

try this

$http.post("/Home/DeleteListItem", data)
.then(function(successResponse){
//Your code to handle response

}, 
function(errorResponse) {
//your code to handle error
});

Upvotes: 0

Related Questions