【问题标题】:Ajax: Pass Model List to ControllerAjax:将模型列表传递给控制器
【发布时间】:2020-01-09 13:06:37
【问题描述】:

我有一个将模型列表作为模型的视图,所以

 @model List<Collections.Models.Status>

我想将此列表作为 Ajax 调用的数据传递,但我不断得到“System.Collections.Generic.List[Collections.Models.Status]”而不是模型中的值。

这是 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);
        }
    }); 

在调试器中翻译为:

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

如何传递列表的实际值而不是 System.Collections.Generic.List[Collections.Models.Status]?

我试过@Model.ToList()、@Html.Raw(Model),都没有成功。

【问题讨论】:

    标签: javascript ajax model-view-controller model


    【解决方案1】:

    将 contentType 属性设置为 application/json。 并使用以下代码设置数据属性

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

    【讨论】:

    • 执行了调用,但没有传递值,当我检查传入的参数时,控制器中的 count = 0。
    猜你喜欢
    • 2013-06-03
    • 2016-03-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 2011-12-16
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多