名单通过在ASP.Net MVC阿贾克斯 [英] List pass through ajax in ASP.Net MVC

查看:133
本文介绍了名单通过在ASP.Net MVC阿贾克斯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过AJAX传递列表。我怎样才能做到这一点,并在运行时赋值。我这样做,但它传递空值。这里是我的code。

I want to pass list through AJAX. How can I do this and assign value on runtime. I am doing it, but it pass null value. Here is my code.

JQuery的:

for (var i = 0; i < 5; i++) {
    aabc += { id: i, color: 'Level' + i } + ",";
}
var str2 = aabc.replace(/,$/, " ")
JSONString3 = [str2];
$.ajax({
    url: '@Url.Action("test123", "ConfigurableTradeLane")',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(JSONString3),
    success: function (data) {
       //alert("success");
    },
    error: function (jqXHR, exception) {
        alert('Error message.');
    }
});

控制器:

public void test123(List<TradelaneDetailViewModel> viewmodel)
        {
            //nothing
        }

发送项值为null。请帮我。

It send "item value is null". Please help me.

推荐答案

假设 TradelaneDetailViewModel 所包含的属性 ID 颜色,那么脚本必须

Assuming TradelaneDetailViewModel contains properties id and color, then the script needs to be

var items = [];
for (var i = 0; i < 5; i++) {
    items.push({ id: i, color: 'Level' + i });
}
$.ajax({
    url: '@Url.Action("test123", "ConfigurableTradeLane")',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({ viewmodel: items }),
    success: function (data) {
       //alert("success");
    },
    error: function (jqXHR, exception) {
        alert('Error message.');
    }
});

这篇关于名单通过在ASP.Net MVC阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆