简体   繁体   English

通过ajax调用传递给mvc控制器时,在列表中获取空值

[英]getting null value in list when passing by ajax call to mvc controller

I am passing my list to an mvc controller but I am getting null value in the controller.But my list has values when show in alert on client side. 我将我的列表传递给mvc控制器,但我在控制器中获取空值。但是当我在客户端显示警报时,我的列表中有值。

ajax call ajax电话

$("#addTiles").click(function() {
    userTiles = JSON.stringify({
        'userTiles': userTiles
    });
    alert("Entered function.");
    alert(userTiles[0].TileID);
    var url = '@Url.Action("AddTiles")';
    $.ajax({
        type: "GET",
        url: url,
        data: userTiles,
        success: function(d) {
            if (d.indexOf('"IsSessionExpired":true') != -1) {
                location.reload();
            } else {
                onAddTilesSuccessful(d);
            }
        },
        error: function() {
            errorInOperation();
        },
        contentType: "application/html; charset=utf-8",
        dataType: 'html'
    });
});

function onAddTilesSuccessful(e) {
    $("#tilesSubmissionMsg").append(e);
}

function errorInOperation(d) {
    $("#tilesSubmissionMsg").append("Something went wrong");
}

mvc controller mvc控制器

  public ActionResult AddTiles(List<UserTilesVM> userTiles)
    {
        return View();
    }

List Model 列表模型

public class UserTilesVM
{
    public int TileID { get; set; }
    public int TopPosition { get; set; }
    public int LeftPosition { get; set; }
}

List in javascript 在javascript中列出

"{"userTiles":[{"TileID":"3","TopPosition":0,"LeftPosition":0}]}"

I have also tried by sending my list with stringfy but that also doesn't work. 我也尝试过用stringfy发送我的列表,但这也行不通。

You have contentType and dataType twice in your AJAX setup, with different values, which will break the AJAX call. 您在AJAX设置中有两次contentType和dataType,具有不同的值,这将打破AJAX调用。

Keep in mind contentType is to tell the server what type of data to expect and dataType is to determine what type of data is returned, from the documentation . 请记住,contentType是告诉服务器期望的数据类型,dataType用于确定从文档中返回的数据类型。

Edit: I see you have edited your code! 编辑:我看到你编辑了你的代码!

In this case, since you are using JSON.Stringify to modify the data you are sending, you would use contentType: "application/json; charset=utf-8", as your contentType, since you are sending JSON data to the backend. 在这种情况下,由于您使用JSON.Stringify来修改要发送的数据,因此您将使用contentType: "application/json; charset=utf-8",作为您的contentType,因为您要将JSON数据发送到后端。

Use : [HttpGet] on the method AddTiles as you have used type: "GET" on the Ajax hit. 使用方法: [HttpGet]的方法AddTiles因为你已经使用type: "GET"在阿贾克斯打。

[HttpGet]
public ActionResult AddTiles(List<UserTilesVM> userTiles)
{
     return View();
}

If Still doesn't works then try type: "POST" on Ajax hit and on method use [HttpPost] 如果仍然不起作用,那么尝试type: "POST" Ajax命中的type: "POST"和方法使用[HttpPost]

[HttpPost]
public ActionResult AddTiles(List<UserTilesVM> userTiles)
{
    return View();
}

when we are trying to pass object data using ajax, we have to store data in variable and pass data directly using "data :'variable'" in AJAX to Controller Method 当我们尝试使用ajax传递对象数据时,我们必须将数据存储在变量中并使用AJAX中的“data:'variable'”直接传递数据到Controller方法

$("#addTiles").click(function() {
   var userTiles = ({
        'userTiles': userTiles
    });
    alert("Entered function.");
    alert(userTiles[0].TileID);
    var url = '@Url.Action("AddTiles")';
    $.ajax({
        type: "POST",
        url: url,
        data: userTiles,
        success: function(d) {
            if (d.indexOf('"IsSessionExpired":true') != -1) {
                location.reload();
            } else {
                onAddTilesSuccessful(d);
            }
        },
        error: function() {
            errorInOperation();
        },
        contentType: "application/html; charset=utf-8",
        dataType: 'html'
    });
});

function onAddTilesSuccessful(e) {
    $("#tilesSubmissionMsg").append(e);
}

function errorInOperation(d) {
    $("#tilesSubmissionMsg").append("Something went wrong");
}

//Use [HttpPost] keyword for getting value which was passed by AJAX. //使用[HttpPost]关键字获取AJAX传递的值。

[HttpPost]
public ActionResult AddTiles(List<UserTilesVM> userTiles)
{
    return View();
} 

I think your list definition is not ok: 我认为你的列表定义不合适:

"{"userTiles":[{"TileID":"3","TopPosition":0,"LeftPosition":0}]}"

should be: 应该:

"{"userTiles":[{"TileID":"3","TopPosition":"0","LeftPosition":"0"}]}"

i have using this sequence that work fine 我使用这个顺序工作正常

you have check the contentType: "application/json", dataType: "json", sequence in ajax method 你检查了contentType:“application / json”,dataType:“json”,ajax方法中的序列

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

相关问题 从Ajax向MVC控制器传递值时为空值 - Null value when passing value from ajax to MVC controller 将列表和字符串数据从Ajax传递到MVC 4中的控制器,列表是否为NULL? - passing List and String data from ajax to controller in mvc 4, getting NULL for list? MVC 4 Ajax异步文件上传始终将空值传递给控制器 - MVC 4 Ajax Asynchronous File Upload Always Passing Null Value To Controller 通过ajax将空值传递给控制器 - Passing null value by ajax to controller Ajax 将空值传递给控制器 - Ajax passing null value to controller MVC Ajax 调用 Controller 方法与参数连接但参数值为 null - MVC Ajax call to Controller Method with Parameter connects but Parameter value is null Jquery ajax将空值传递给MVC控制器 - Jquery ajax passing null values to MVC controller ASP.NET MVC通过Java序列化到Controller的AJAX通过BeginCollectionItem List的对象传递null - ASP.NET MVC Passing a BeginCollectionItem List's Object through AJAX with Javascript Serialize to Controller is passing null ajax 数据在 controller ZC1C425268E68385D1AB5074C17A9 中获取 null - ajax data is getting null in controller function of MVC 为什么我无法通过AJAX传递给MVC控制器的XML到达那里? - Why is XML I'm passing via AJAX to an MVC controller not getting there?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM