简体   繁体   English

Ajax:将模型列表传递给控制器

[英]Ajax: Pass Model List to Controller

I have a view that takes a list of a model as the Model, so 我有一个将模型列表作为模型的视图,所以

 @model List<Collections.Models.Status>

I want to pass this list as the data for an Ajax call, but I keep getting "System.Collections.Generic.List[Collections.Models.Status]" instead of the values in the model. 我想将此列表作为Ajax调用的数据传递,但是我一直在获取“ System.Collections.Generic.List [Collections.Models.Status]”而不是模型中的值。

Here's the Ajax code: 这是Ajax代码:

$.ajax({
        url: "/Home/Update",
        data: JSON.stringify(@Model),
        type: 'POST',
        dataType: 'json',
        success: function (responseJSON) {
            if (responseJSON.message == 'success') {
                alert('here');
            }
        },
        error: function (error) {
            showModal("Error: " + error.message);
        }
    }); 

Which translates in the debugger to: 在调试器中将其转换为:

$.ajax({
        url: "/Home/Update",
        data: JSON.stringify(System.Collections.Generic.List`1[Collections.Models.CollectionStatus]),
        type: 'POST',
        dataType: 'json',
        success: function (responseJSON) {
            if (responseJSON.message == 'success') {
                alert('here');
            }
        },
        error: function (error) {
            showModal("Error: " + error.message);
        }
    });

How do I pass the actual values of the list instead of System.Collections.Generic.List[Collections.Models.Status]? 如何传递列表的实际值而不是System.Collections.Generic.List [Collections.Models.Status]?

I've tried @Model.ToList(), @Html.Raw(Model), neither worked. 我尝试了@ Model.ToList(),@ Html.Raw(Model),但都没有用。

set contentType property to application/json. 将contentType属性设置为application / json。 and use below code to set data property 并使用下面的代码设置数据属性

data: JSON.stringify({'your controllers parameter name': '@Model'})

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

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