1

send an object with a quantity of data from the View to the Controller in ASP.net MVC and AngularJS

VIEW

var Person = {};
Person.IdPerson = 69425;
Person.Year = new Date().getFullYear();

$http.post('/API/Update_Person', { params: { Person: Person } }).then(function (response) {

});

CONTROLLER

public JsonResult Update_Person(ModelPerson Person)
{
    var person = (from c in db.Person where c.IdPerson == Person.IdPerson && c.Year == Person.Year select c).FirstOrDefault();

    return new JsonResult { Data = "OK", JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}

more effective method than this?

$http({
    method: "post",
    url: "/API/Person",
    datatype: "json",
    data: JSON.stringify(Person)
}).then(function (response) {

});
3
  • 2
    What is the question? Commented Aug 9, 2019 at 14:55
  • how send an object from view to Controller AngularJS + ASP.net MVC? @nico_c Commented Aug 9, 2019 at 15:05
  • You need to elaborate it better. I cant understand it Commented Aug 10, 2019 at 10:46

1 Answer 1

1

You can follow the below Approach for passing the data to API controller:

var Person = {};
Person.IdPerson = 69425;
Person.Year = new Date().getFullYear();

$http.post('/API/Update_Person', Person, {
                    headers: {
                        'Content-Type': 'application/json'
                    }
                }).then(function (response) {
                    console.log(response);
                }, function(error){
                    console.log(error);
                });

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.