How to send IEnumerable list from Ajax to Controller

十年热恋 提交于 2020-01-13 05:02:33

问题


I have a web application in MVC3 and i'm using Telerik Grid Batch Editing.

Batch Editing have save changes button which returns UPDATED COLUMNS to controller IEnumerable list like

    [GridAction]
    public ActionResult Update(IEnumerable<Customers> updated)
    {
        ///user codes
    }

but how to collect updated rows and make array send like IEnumerable list from Javascript with ajax to Controller ?

EDIT I'm putting my view png

I just want to send updated rows data to Controller and Save Changes button can do this but before thje send values i just want to ask to user "Are you sure to Load?" and after the send data I want to refresh all the page

So i thinked to do this with ajax request because i'm also using batch editing with ajax requests

Do you have any exprience for this situation?


回答1:


Use the AJAX POST as I have used in my Tested Javascript function as::

function TestAjax() {
    var Test = [];

    for (var i = 0; i < 5; i++) {
        Test.push({ ID: i, Name: "RJ" });
    }

    $.ajax({
        type: 'POST',
        url: rootUrl('Home/TestPost'),
        contentType: "application/json",
        //data: { Test: JSON.stringify( data) },
        data:JSON.stringify( {Test: Test}),
        success: function (data) {
            alert("Succeded");
        }
    });
}

And on Server Side(i.e. In Controller) use something Like::

public ActionResult TestPost(IEnumerable<TestViewModel> Test)
    {
        return Json(3);
    }

The ViewModel Contains different propeties which are of different datatypes as::

public class TestViewModel
    {
        public long ID { get; set; }
        public string Name { get; set; }
    }

This is working fine. May be this will help you.



来源:https://stackoverflow.com/questions/22345335/how-to-send-ienumerable-list-from-ajax-to-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!