我已经淘汰了包含大量数据的网页,并上传了一系列图片。 最后,有一个带有send方法的按钮,我想将我的所有数据和图像发送到Wep Api控制器。 我一直在努力传递这些图像,无论我尝试它发送一个空数组。该页面不包含表单,因此我也无法尝试发送新的FormData对象。
按钮:
<button class="btn btn-info" data-bind="click: fogBugzForm.send">Submit</button>
发送方式:
this.send = () => {
var data = {
screenshots: this.fogBugzData().screenshots(), // knockoutObservableArray<string>
otherStringData: this.fogBugzData().otherStringData()
};
if (this.fogBugzData.isValid()) {
$.ajax({
type: "POST",
data: ko.toJSON(data),
url: "api/FogBuzReporter/ReportBug",
contentType: "application/json"
});
Web API控制器:
[HttpPost]
public void ReportBug([FromBody] FogBuzModel bug)
{
...
}
public class FogBuzModel
{
public string[] screenshots { get; set; }
public string otherStringData {get;set;}
}
任何帮助都会很棒:)
答案 0 :(得分:0)
self.data = [
{ Name: "Nokia Lumia", price: 10000, Image: "Images/Phones/lumia.jpg" },
{ Name: "Xiaomi MI3", price: 9999.99, Image: "Images/Phones/mi3.jpg" },
{ Name: "Apple Iphone", price: 50000, Image: "Images/Phones/iphone.jpg" },
{ Name: "HTC One", price: 40000, Image: "Images/Phones/htc.jpg" },
{ Name: "OnePlus One", price: 22990, Image: "Images/Phones/oneplus.jpg" },
{ Name: "Sony Xperia", price: 30000, Image: "Images/Phones/xperia.jpg" },
{ Name: "Galaxy Note4", price: 48000, Image: "Images/Phones/note.jpg" }
];
$.ajax({
url: "@Url.Action("WriteData","Home")",
type: "post",
data: JSON.stringify(self.data[0]),
dataType: "json",
contentType: "application/json",
success: function (question) {
bootbox.alert("Product saved successfully");
},
error: function () {
alert("Something went wrong");
}
});
嗨,我在你的问题中写的没有看到任何问题,但再次检查。 我正在使用这段代码将一些数据发送到Home Controller中的方法“WriteData”(在这里使用MVC)。
WriteData 看起来像:
[HttpPost]
public string WriteData(ShopItems item)
{
Product ob = new Product();
bool result=ob.Write(item);
if (result)
return "true";
else return "false";
}
和ShopItems:
public class ShopItems
{
[Key]
public int Id { get; set; }
public int Type { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public double price { get; set; }
public string Image { get; set; }
}
这整个代码工作得很完美。尝试一次,请告诉我。