使用post将Javascript数组传递给后端

时间:2016-12-01 12:17:50

标签: javascript c# angularjs

我正在尝试将javascript数组传递给后端。但显然,我并不期待后端的正确类型。我应该期待什么类型?我也添加了删除方法。我是否需要迭代此数组才能删除记录?

[{"questionId":135,"answer":"A6.1"},{"questionId":135,"answer":"A6.2"}]

function deleteAnswers(arrayToDelete) {
    $http.post(baseUrl + "Admin/deleteAnswers", {data: arrayToDelete})
        .success(function (data, status, headers, config) {})
}


[HttpPost]
public ActionResult deleteAnswers(string  data) {
    Console.Write(data);
    Response.StatusCode = 200;
    return Content("Sucess");
}


public void deleteAnswers(dynamic answersToDelete) {
    try {
        using (TestEntities testEntity = new TestEntities()) {
            foreach (var item in answersToDelete) {
                Console.Write(item.);
                //remove from 
            }
            testEntity.SaveChanges();
        }
    } catch (Exception ex) {
        Console.Write(ex);
    }
}

1 个答案:

答案 0 :(得分:0)

您发送的对象的属性data带有数组值,因此您的签名应如下所示:

[HttpPost]
public ActionResult deleteAnswers(MyObject data)
{
    Console.Write(data);
    Response.StatusCode = 200;
    return Content("Sucess");
}

public class MyObject {
    public List<Answer> Data { get; set; }
}

public class Answer {
    public int QuestionId { get; set; }
    public string Answer { get; set; }
}