0

So, I have an angularJS array and I want to pass it to ASP.Net MVC method and then store its data in a database.

The array looks like the following:

telephone = [{'id':'T1', 'contactN':'212-289-3824'}, {'id':'T2', 'contactN':'212-465-1290'}];

When I click on a button, it fires the following JS function:

$scope.updateUserContacts = function () {
    $http.post('/Home/UpdateUserContacts', { contactsData: $scope.telephone })
        .then(function (response) {
            $scope.users = response.data;
        })
    .catch(function (e) {
        console.log("error", e);
        throw e;
    })
    .finally(function () {
        console.log("This finally block");
    });
}

My question is, how I can receive this array in my ASP.Net MVC? What the format that can be compatible with this array?

The below is an example of ASP.Net MVC method but I don't know what the type and/or how to receive the passed array??

[HttpPost] //it means this method will only be activated in the post event
    public JsonResult UpdateUserContacts(??? the received array)
    {
        ......
}

2 Answers 2

2

In your MVC Application you should have Telephone Class

class Telephone 
{
  public string id;
  public string contactN;
}

[HttpPost] //it means this method will only be activated in the post event
public JsonResult UpdateUserContacts(Telephone[] contactsData)
 {
        //Do something...
 }
Sign up to request clarification or add additional context in comments.

Comments

1

Type should be List or Array

[HttpPost] //it means this method will only be activated in the post event
    public JsonResult UpdateUserContacts(List<MyObj> contactsData)
    {
        ......
    }

OR

   public JsonResult UpdateUserContacts(MyObj[] contactsData)
    {
        ......
    }

And you should have model class like this

public class MyObj
{
  public string id {get;set;}
  public string contactN {get;set;}
}

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.