简体   繁体   中英

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.

Here's the Ajax code:

$.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]?

I've tried @Model.ToList(), @Html.Raw(Model), neither worked.

set contentType property to application/json. and use below code to set data property

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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