jQuery Ajax를 사용하여 MVC 컨트롤러 메서드에 개체 목록 전달


113

jQuery의 ajax () 함수를 사용하여 MVC 컨트롤러 메서드에 개체 배열을 전달하려고합니다. PassThing () C # 컨트롤러 메서드에 들어가면 "things"인수가 null입니다. 인수에 List 유형을 사용하여 시도했지만 작동하지 않습니다. 내가 뭘 잘못하고 있죠?

<script type="text/javascript">
    $(document).ready(function () {
        var things = [
            { id: 1, color: 'yellow' },
            { id: 2, color: 'blue' },
            { id: 3, color: 'red' }
        ];

        $.ajax({
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            type: 'POST',
            url: '/Xhr/ThingController/PassThing',
            data: JSON.stringify(things)
        });
    });
</script>

public class ThingController : Controller
{
    public void PassThing(Thing[] things)
    {
        // do stuff with things here...
    }

    public class Thing
    {
        public int id { get; set; }
        public string color { get; set; }
    }
}


3
데이터는 문자열이지만 메서드는 배열을 허용합니다. 문자열을 허용하도록 메서드를 변경 한 다음 메서드 내에서 역 직렬화합니다.
Bob Horn

2
귀하의 코드가 정확합니다. 나는 그것을 테스트했고 그것은 MVC 4를 사용하여 작동했습니다. 그것을 알아 내기 위해 더 많은 데이터를 제공하십시오.
디에고

이것은 대단한 일이지만 전달할 문자열 목록뿐만 아니라 문자열 목록과 관련된 별도의 ID를 포함해야하는 경우에는 어떻게해야합니까? 예를 들어, 그룹 ID, 그룹 ID 아래의 그룹 목록.
Nathan McKaskle

답변:


188

NickW의 제안을 사용하여 things = JSON.stringify({ 'things': things });Here is the complete code를 사용하여이 작업을 수행 할 수있었습니다 .

$(document).ready(function () {
    var things = [
        { id: 1, color: 'yellow' },
        { id: 2, color: 'blue' },
        { id: 3, color: 'red' }
    ];      

    things = JSON.stringify({ 'things': things });

    $.ajax({
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'POST',
        url: '/Home/PassThings',
        data: things,
        success: function () {          
            $('#result').html('"PassThings()" successfully called.');
        },
        failure: function (response) {          
            $('#result').html(response);
        }
    }); 
});


public void PassThings(List<Thing> things)
{
    var t = things;
}

public class Thing
{
    public int Id { get; set; }
    public string Color { get; set; }
}

이것에서 배운 두 가지가 있습니다.

  1. contentType 및 dataType 설정은 ajax () 함수에서 절대적으로 필요합니다. 누락 된 경우 작동하지 않습니다. 많은 시행 착오 끝에 이것을 발견했습니다.

  2. MVC 컨트롤러 메서드에 개체 배열을 전달하려면 JSON.stringify ({ 'things': things}) 형식을 사용하면됩니다.

다른 사람에게 도움이되기를 바랍니다.


8
나는 같은 문제가 있었고 contentType을 추가하여 수정했습니다. 감사!
Rochelle C

9
두 가지 참고 : JSON.stringify 및 'contentType'지정.
dinesh ygv 2014

Crud. 여전히 나를 위해 일하지 않습니다. 내 요청 URL은 http://localhost:52459/Sales/completeSale?itemsInCart=[{"ItemId":1,"Quantity":"1","Price":3.5}]하고 Sales.completeSale있다 public ActionResult completeSale(ItemInCart[] itemsInCart), A와 주석 HttpGet.
abalter

3
어떤 이유에서든지 그냥 사용해야했습니다data: JSON.stringify(things),
Rob Scott

1
dataType필요가 없습니다. 그 생략 된 경우, 아약스 함수는 반환 데이터를 기반으로 그것을 밖으로 작동합니다

32

그냥 할 수 없나요?

var things = [
    { id: 1, color: 'yellow' },
    { id: 2, color: 'blue' },
    { id: 3, color: 'red' }
];
$.post('@Url.Action("PassThings")', { things: things },
   function () {
        $('#result').html('"PassThings()" successfully called.');
   });

... 그리고 당신의 행동을

[HttpPost]
public void PassThings(IEnumerable<Thing> things)
{
    // do stuff with things here...
}

3
이것이 최선의 답이 될 것입니다. JSON.stringify이 경우 사용할 수 없습니다

이것은 나를 위해 작동하지 않습니다 .. [HttpPost] public int SaveResults (List <ShortDetail> model) {} 및 $ .post ( "@ Url.Action ("SaveResults ","Maps ")", {model : dataItems}, 함수 (결과) {});
삼라

2
그것은 나를 위해 일했습니다. 절대적으로 최고의 대답입니다. Halcyon 구현이 작동하지 않는 이유를 모르겠습니다. PassThings 함수가 호출되었지만 'things'입력 변수가 호출 직전에 자바 스크립트에 채워져 있어도 비어있었습니다.
Leonardo Daga

13

문제가 될 수있는 데이터 서식 지정. 다음 중 하나를 시도하십시오.

data: '{ "things":' + JSON.stringify(things) + '}',

또는 ( 폼없이 ASP.NET MVC 컨트롤러에 문자열 배열어떻게 게시 할 수 있습니까? )

var postData = { things: things };
...
data = postData

코드가 가깝지만 작동하지 않습니다. 귀하의 제안 덕분에 코드가 작동하도록 할 수있었습니다. 위의 내 대답을 참조하십시오.
Halcyon 2012

12

.Net Core 2.1 웹 응용 프로그램을 사용하고 있으며 여기에서 하나의 답변을 얻을 수 없습니다. 빈 매개 변수 (메서드가 호출 된 경우) 또는 500 서버 오류가 발생했습니다. 나는 가능한 모든 답변 조합을 가지고 놀기 시작했고 마침내 작업 결과를 얻었습니다.

제 경우에는 해결책은 다음과 같습니다.

스크립트-원래 배열 문자열 화 (명명 된 속성을 사용하지 않음)

    $.ajax({
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        url: mycontrolleraction,
        data: JSON.stringify(things)
    });

그리고 컨트롤러 메서드에서 [FromBody]를 사용합니다.

    [HttpPost]
    public IActionResult NewBranch([FromBody]IEnumerable<Thing> things)
    {
        return Ok();
    }

실패에는 다음이 포함됩니다.

  • 콘텐츠 이름 지정

    data : {content : nodes}, // 서버 오류 500

  • contentType = 서버 오류 500이 없음

노트

  • dataType응답 디코딩에 사용 되기 때문에 일부 답변에도 불구하고 필요하지 않습니다 (따라서 요청 여기 예제 ).
  • List<Thing> 컨트롤러 방법에서도 작동합니다.

10

나는이 모든 것에 대한 완벽한 대답을 가지고 있습니다 : 마침내 스스로 관리 할 수없는 많은 솔루션을 시도했습니다. 아래에서 자세한 답변을 찾으십시오.

       $.ajax({
            traditional: true,
            url: "/Conroller/MethodTest",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data:JSON.stringify( 
               [
                { id: 1, color: 'yellow' },
                { id: 2, color: 'blue' },
                { id: 3, color: 'red' }
                ]),
            success: function (data) {
                $scope.DisplayError(data.requestStatus);
            }
        });

관제사

public class Thing
{
    public int id { get; set; }
    public string color { get; set; }
}

public JsonResult MethodTest(IEnumerable<Thing> datav)
    {
   //now  datav is having all your values
  }

더 많은
업 보트

7

이 작업을 수행 할 수있는 유일한 방법은 JSON을 문자열로 전달한 다음을 사용하여 역 직렬화하는 것입니다 JavaScriptSerializer.Deserialize<T>(string input). 이는 MVC 4의 기본 역 직렬화 기인 경우 매우 이상합니다.

내 모델에는 중첩 된 개체 목록이 있으며 JSON 데이터를 사용하여 얻을 수있는 최선의 방법은 올바른 항목 수를 포함하는 최상위 목록이지만 항목의 모든 필드는 null이었습니다.

이런 종류의 일은 그렇게 어렵지 않아야합니다.

    $.ajax({
        type: 'POST',
        url: '/Agri/Map/SaveSelfValuation',
        data: { json: JSON.stringify(model) },
        dataType: 'text',
        success: function (data) {

    [HttpPost]
    public JsonResult DoSomething(string json)
    {
        var model = new JavaScriptSerializer().Deserialize<Valuation>(json);

이 작업을 수행하려면 Ajax 호출 형식을 면밀히 따르십시오.
Graham Laight

4

이것은 귀하의 쿼리에 대한 작동 코드이며 사용할 수 있습니다.

관제사

    [HttpPost]
    public ActionResult save(List<ListName> listObject)
    {
    //operation return
    Json(new { istObject }, JsonRequestBehavior.AllowGet); }
    }

자바 스크립트

  $("#btnSubmit").click(function () {
    var myColumnDefs = [];
    $('input[type=checkbox]').each(function () {
        if (this.checked) {
            myColumnDefs.push({ 'Status': true, 'ID': $(this).data('id') })
        } else {
            myColumnDefs.push({ 'Status': false, 'ID': $(this).data('id') })
        }
    });
   var data1 = { 'listObject': myColumnDefs};
   var data = JSON.stringify(data1)
   $.ajax({
   type: 'post',
   url: '/Controller/action',
   data:data ,
   contentType: 'application/json; charset=utf-8',
   success: function (response) {
    //do your actions
   },
   error: function (response) {
    alert("error occured");
   }
   });

2

MVC 컨트롤러에서 예상하는 매개 변수 이름과 일치하는 속성을 포함하는 다른 개체로 개체 목록을 래핑하면 작동합니다. 중요한 것은 개체 목록을 둘러싼 래퍼입니다.

$(document).ready(function () {
    var employeeList = [
        { id: 1, name: 'Bob' },
        { id: 2, name: 'John' },
        { id: 3, name: 'Tom' }
    ];      

    var Employees = {
      EmployeeList: employeeList
    }

    $.ajax({
        dataType: 'json',
        type: 'POST',
        url: '/Employees/Process',
        data: Employees,
        success: function () {          
            $('#InfoPanel').html('It worked!');
        },
        failure: function (response) {          
            $('#InfoPanel').html(response);
        }
    }); 
});


public void Process(List<Employee> EmployeeList)
{
    var emps = EmployeeList;
}

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
}

1
     var List = @Html.Raw(Json.Encode(Model));
$.ajax({
    type: 'post',
    url: '/Controller/action',
    data:JSON.stringify({ 'item': List}),
    contentType: 'application/json; charset=utf-8',
    success: function (response) {
        //do your actions
    },
    error: function (response) {
        alert("error occured");
    }
});

ajax를 사용하여 List Of 모델 객체를 전달하려면이 코드를 사용해보십시오. Model은 IList <Model>을 나타냅니다. 컨트롤러에서 IList <Model>을 사용하여 값을 가져옵니다.
Athul Nalupurakkal

0

ASP.NET Web API를 사용하는 경우 data: JSON.stringify(things).

컨트롤러는 다음과 같이 보일 것입니다.

public class PassThingsController : ApiController
{
    public HttpResponseMessage Post(List<Thing> things)
    {
        // code
    }
}

0

@veeresh i에서 수정

 var data=[

                        { id: 1, color: 'yellow' },
                        { id: 2, color: 'blue' },
                        { id: 3, color: 'red' }
                        ]; //parameter
        var para={};
        para.datav=data;   //datav from View


        $.ajax({
                    traditional: true,
                    url: "/Conroller/MethodTest",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    data:para,
                    success: function (data) {
                        $scope.DisplayError(data.requestStatus);
                    }
                });

In MVC



public class Thing
    {
        public int id { get; set; }
        public string color { get; set; }
    }

    public JsonResult MethodTest(IEnumerable<Thing> datav)
        {
       //now  datav is having all your values
      }

0

DataTable에서 선택한 여러 행의 일부 데이터를 MVC 작업으로 보내려고 할 때 수행 한 작업 :

HTML 페이지 시작 부분 :

@Html.AntiForgeryToken()

(행만 표시되며 모델에서 바인딩) :

 @foreach (var item in Model.ListOrderLines)
                {
                    <tr data-orderid="@item.OrderId" data-orderlineid="@item.OrderLineId" data-iscustom="@item.IsCustom">
                        <td>@item.OrderId</td>
                        <td>@item.OrderDate</td>
                        <td>@item.RequestedDeliveryDate</td>
                        <td>@item.ProductName</td>
                        <td>@item.Ident</td>
                        <td>@item.CompanyName</td>
                        <td>@item.DepartmentName</td>
                        <td>@item.ProdAlias</td>
                        <td>@item.ProducerName</td>
                        <td>@item.ProductionInfo</td>
                    </tr>
                }

JavaScript 기능을 시작하는 버튼 :

 <button class="btn waves-effect waves-light btn-success" onclick="ProcessMultipleRows();">Start</button>

자바 스크립트 기능 :

  function ProcessMultipleRows() {
            if ($(".dataTables_scrollBody>tr.selected").length > 0) {
                var list = [];
                $(".dataTables_scrollBody>tr.selected").each(function (e) {
                    var element = $(this);
                    var orderid = element.data("orderid");
                    var iscustom = element.data("iscustom");
                    var orderlineid = element.data("orderlineid");
                    var folderPath = "";
                    var fileName = "";

                    list.push({ orderId: orderid, isCustomOrderLine: iscustom, orderLineId: orderlineid, folderPath: folderPath, fileName : fileName});
                });

                $.ajax({
                    url: '@Url.Action("StartWorkflow","OrderLines")',
                    type: "post", //<------------- this is important
                    data: { model: list }, //<------------- this is important
                    beforeSend: function (xhr) {//<--- This is important
                      xhr.setRequestHeader("RequestVerificationToken",
                      $('input:hidden[name="__RequestVerificationToken"]').val());
                      showPreloader();
                    },
                    success: function (data) {

                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {

                    },
                     complete: function () {
                         hidePreloader();
                    }
                });
            }
        }

MVC 작업 :

[HttpPost]
[ValidateAntiForgeryToken] //<--- This is important
public async Task<IActionResult> StartWorkflow(IEnumerable<WorkflowModel> model)

C #의 MODEL :

public class WorkflowModel
 {
        public int OrderId { get; set; }
        public int OrderLineId { get; set; }
        public bool IsCustomOrderLine { get; set; }
        public string FolderPath { get; set; }
        public string FileName { get; set; }
 }

결론:

오류의 이유 :

"Failed to load resource: the server responded with a status of 400 (Bad Request)"

Is 속성 : [ValidateAntiForgeryToken]MVC 작업의 경우StartWorkflow

Ajax 호출 솔루션 :

  beforeSend: function (xhr) {//<--- This is important
                      xhr.setRequestHeader("RequestVerificationToken",
                      $('input:hidden[name="__RequestVerificationToken"]').val());
                    },

객체 목록을 보내려면 예제 (목록 객체 채우기)와 같이 데이터를 형성해야합니다.

데이터 : {모델 : 목록},

유형 : "포스트",


0

이것이 나에게 잘 작동하는 방법입니다.

var things = [
    { id: 1, color: 'yellow' },
    { id: 2, color: 'blue' },
    { id: 3, color: 'red' }
];

$.ajax({
    ContentType: 'application/json; charset=utf-8',
    dataType: 'json',
    type: 'POST',
    url: '/Controller/action',
    data: { "things": things },
    success: function () {
        $('#result').html('"PassThings()" successfully called.');
    },
    error: function (response) {
        $('#result').html(response);
    }
});

대문자 "C"의 "ContentType"사용.

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.