简体   繁体   English

使用ajax将列表发送到mvc控制器

[英]send List into mvc controller with ajax

I have a List which look like the following我有一个如下所示的列表

列表

I want to send this list into my controller,我想将此列表发送到我的控制器中,

I am using ajax call to send data from client side to server side我正在使用 ajax 调用将数据从客户端发送到服务器端

here is my ajax call这是我的 ajax 调用

    $.ajax({
        url: '/Main/updateTripundHoliday',
        data: d.weekendLeave,
        type: "GET",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (response) {

        }
    });

and my method in controller和我在控制器中的方法

    public bool updateHoliday(List<Holidaysclass> data)
    {
        for (var i = 0; i < data.Count(); i++)
        {
            insertHolidays(data.ElementAt(i).Date, data.ElementAt(i).Day, data.ElementAt(i).HolidayName, data.ElementAt(i).isActive, data.ElementAt(i).currentYear, data.ElementAt(i).isHolidayWeekend, data.ElementAt(i).OfficialID);
        }
        return true;
    }

here my List<Holidaysclass> data is showing null这里我的List<Holidaysclass> data显示为空

what can I do here?我能在这里做什么?

To send data from browser to controller you need to use POST type and then pass data inside ajax call.要将数据从浏览器发送到控制器,您需要使用POST类型,然后在 ajax 调用中传递data you can directly map your entites in action method.您可以直接在操作方法中映射您的entites

 $.ajax({
        url: '/Main/updateTripundHoliday',
        data: d.weekendLeave,
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (response) {

        }
    });

and in controller put HttpPost Data Annotation并在控制器中放置HttpPost数据注释

 [HttpPost]
 public bool updateHoliday(List<Holidaysclass> data)
    {
        for (var i = 0; i < data.Count(); i++)
        {
            insertHolidays(data.ElementAt(i).Date, data.ElementAt(i).Day, data.ElementAt(i).HolidayName, data.ElementAt(i).isActive, data.ElementAt(i).currentYear, data.ElementAt(i).isHolidayWeekend, data.ElementAt(i).OfficialID);
        }
        return true;
    }

Using ajax get method we cant send data from client to server is not best way.使用 ajax get 方法我们不能将数据从客户端发送到服务器不是最好的方法。 try using POST method send data from client to server.尝试使用 POST 方法将数据从客户端发送到服务器。

Reference : https://api.jquery.com/jquery.post/参考: https : //api.jquery.com/jquery.post/

 $.ajax({
    url: '/Main/updateTripundHoliday',
    data: d.weekendLeave,
    type: "POST",
    ......
});

You can do it like this :你可以这样做:

$(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);
        }
    }); 
});

Please follow this link for more information : link请点击此链接了解更多信息:链接

You can not post data in get request.您不能在获取请求中发布数据。 Instead you need to use POST type request.相反,您需要使用POST类型的请求。 Here is your updated request.这是您更新的请求。

$.ajax({
        url: '/Main/updateTripundHoliday',
        data: d.weekendLeave,
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (response) {

        }
    });

Also your action should have [HttpPost] annotation.此外,您的操作应该有[HttpPost]注释。

try this:尝试这个:

$.ajax({
        url: '/Main/updateHoliday',
        data: {list: d.weekendLeave},
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (response) {

        }
    });

[HttpPost]    
public bool updateHoliday(List<Holidaysclass> list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM