简体   繁体   English

对于控制器的 Ajax Post 方法,参数作为空值传入

[英]Parameters are coming in as null for Ajax Post method to Controller

I have this bit of code that triggers on click.我有这段代码可以在点击时触发。 When I set a breakpoint at SaveNewProduct all the values are null.当我在SaveNewProduct处设置断点时,所有值都为空。 I tried to create a input object, I tried to add in each property manually, nothing worked.我试图创建一个输入对象,我试图手动添加每个属性,但没有任何效果。 May I get any tips or suggestion.我可以得到任何提示或建议吗?

var input = {
    name: name,
    type: type,
    description: description,
    category: category,
    file: file.files[0],
    acronym: acronym
};

$.ajax({
    type: "POST",
    url: '/Admin/SaveNewProduct',

    processData: false,
    data: {
        name: name,
        type: type,
        description: description,
        category: category,
        file: file.files[0],
        acronym: acronym,
        input: input
    },
    success: function (response) {
        alert("saved okay");
    }
});

[HttpPost]
public async Task<ActionResult> SaveNewProduct(SaveNewProductInput input)
{
    ...
    //breakpoint here, input values are all null
    ...
}

SaveNewProductInput.cs保存新产品输入.cs

public class SaveNewProductInput
{
    public string Name { get; set; }
    public string Acronym { get; set; }
    public string Type { get; set; }
    public string Category { get; set; }
    public string Description { get; set; }
    public HttpPostedFileBase File { get; set; }
}

I also tried remove processData, i Am presented with this error Uncaught TypeError: Illegal invocation我也尝试删除 processData,但出现此错误Uncaught TypeError: Illegal invocation

You need to use FormData to send files in request with processData and contentType set to false您需要使用FormData发送请求中的文件,并将processDatacontentType设置为false

var formData = new FormData();
formData.append("name", name);
formData.append("type", type);
formData.append("description", description);
formData.append("category", category);
formData.append("file", file.files[0]);
formData.append("acronym", acronym);

$.ajax({
    url: "/Admin/SaveNewProduct",
    type: "POST",
    data: formData,        
    processData: false,
    contentType: false,
    success: function (response) {
        alert("saved okay");
    }
});

JQuery automatically converts the 'data' object into request parameters, but I think it not doing that because you set 'processData' to false. JQuery 自动将“数据”对象转换为请求参数,但我认为它不会这样做,因为您将“processData”设置为 false。 Please remove processData and try.请删除 processData 并尝试。

var input = {},
input.Name = name;
input.Type = type;
input.Description = description;
input.Category = category;
input.File = file.files[0];
input.Acronym = acronym;
$.ajax({
    url: "/Admin/SaveNewProduct",
    type: "POST",
    data: JSON.stringify(input),
    dataType: "json",
    contentType: 'application/json; charset=utf-8',
    success: function (response) {
    }
});

[HttpPost]
public async Task<ActionResult> SaveNewProduct(SaveNewProductInput input)
{
    ...
}

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

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