我没有在 web api post 上找到太多信息。这是我发现的一篇博客文章,其中讨论了如何从淘汰赛中进行 POST。带有 KnockoutJs ViewModel 的 Web Api POST
视图模型:
<script type="text/javascript">
var QuickEntry = function (_itemPartNumb, _itemDescription, _itemQuanities) {
this.ItemPartNumb = ko.observable(_itemPartNumb);
this.ItemDescription = ko.observable(_itemDescription);
this.ItemQuanties = ko.observable(_itemQuanities);
};
function QuickEntriesViewModel() {
var self = this;
self.quickEntries = ko.observableArray([]);
for (var i = 0; i < 10; i++) {
self.quickEntries.push(new QuickEntry());
}
self.addNewRow = function () {
self.quickEntries.push(new QuickEntry());
}.bind(self);
self.addToCart = function() {
var items = ko.toJSON(self);
$.ajax({
url: '/DesktopModules/blah/API/Data/Post',
type: 'POST',
data: items,
datatype: "json",
processData: false,
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data);
},
statusCode: {
404: function () {
alert('Failed');
}
}
});
};
};
ko.applyBindings(new QuickEntriesViewModel());
数据控制器(Web API)
[HttpPost]
public string Post(quickEntries values)
{
string response = string.Empty;
response = values.Items != null ? "some data" : "nothing at all";
return response;
}
//class
public class quickEntries
{
public MyQuickEntry[] Items { get; set; }
}
public class MyQuickEntry
{
public string ItemPartNumb { get; set; }
public string ItemDescription { get; set; }
public string ItemQuanties { get; set; }
}
这是从提琴手传递给 web api POST 方法的内容:
{"quickEntries":[{"ItemPartNumb":"bob","ItemDescription":"bob","ItemQuanties":"bob"},{},{},{},{},{},{}, {}、{}、{}]}
有没有人有将 json 对象数组传递给 web api 的经验?