javascript 将knockout.js observablearray 对象传递给MVC 控制器操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12846689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing knockout.js observablearray object to MVC Controller Action?
提问by ccorrin
Im using knockout with MVC. Im trying to pass an observable array of objects from knockout back to my MVC controller action for saving to the database. If I pass the Array from knockout over to my controller action via ko.toJSON(viewModel.ArrayName) it comes back as null in my controller parameter. If I try to pass it to MVC via ko.toJS(viewModel.ArrayName) it has the correct number of items but the data is null for some reason. Any help on how to do this would be greeatly appreciated. Thanks!
我在 MVC 中使用淘汰赛。我试图将一个可观察的对象数组从淘汰赛传递回我的 MVC 控制器操作以保存到数据库。如果我通过 ko.toJSON(viewModel.ArrayName) 将 Array 从敲除传递给我的控制器操作,它会在我的控制器参数中返回为 null。如果我尝试通过 ko.toJS(viewModel.ArrayName) 将它传递给 MVC,它具有正确数量的项目,但由于某种原因数据为空。任何有关如何做到这一点的帮助将不胜感激。谢谢!
My JQuery Data Retrieval Method:
我的 JQuery 数据检索方法:
var dataService = {};
var viewModel;
$(document).ready(function () {
dataService.getAccessLevelList();
})
dataService.getAccessLevelList = function () {
$.post('/DataService/GetAccessLevelList', null, function (data) {
viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
}
This is the problem method:
这是问题方法:
updateAccessLevels = function () {
$.post('/DataService/UpdateAccessLevels', { accessLevels: ko.toJSON(viewModel.AccessLevels) }, function (status) {
alert(status);
});
}
My MVC Controller Data Retrieval action:
我的 MVC 控制器数据检索操作:
[HttpPost]
public ActionResult GetAccessLevelList()
{
FacilityAccessViewModel viewModel = new FacilityAccessViewModel();
viewModel.AccessLevels = unitOfWork.AccessLevelRepository.Get().ToList();
return Json(viewModel);
}
The parameter is coming back NULL or with NULL data when trying to pass it in from Knockout on this controller method.
尝试在此控制器方法上从 Knockout 传入参数时,参数返回 NULL 或带有 NULL 数据。
[HttpPost]
public ActionResult UpdateAccessLevels(List<AccessLevel> accessLevels)
{
try
{
foreach (AccessLevel record in accessLevels)
{
unitOfWork.AccessLevelRepository.Update(record);
}
unitOfWork.Save();
return Json("SUCCESS");
}
catch (Exception ex)
{
return Json(ex.ToString());
}
}
Below is the JSON data shown via fiddler thats being posted to my MVC controller action when i pass in { accessLevels: ko.toJSON(viewModel.AccessLevels) } the controller parameter is coming in null using this
下面是通过 fiddler 显示的 JSON 数据,当我传入 { accessLevels: ko.toJSON(viewModel.AccessLevels) } 时,控制器参数将使用此参数传入我的 MVC 控制器操作
[{"Access":[],"Id":1,"AccessLevelDesc":"TEST222"},{"Access":[],"Id":2,"AccessLevelDesc":"TEST222"}]
Below is the JS data shown via fiddler thats being posted to my MVC controller action when i pass in { accessLevels: ko.toJS(viewModel.AccessLevels) }, in this case the list has two members as expected, but the data is all null in the properties
下面是通过 fiddler 显示的 JS 数据,当我传入 { accessLevels: ko.toJS(viewModel.AccessLevels) } 时,该数据被发布到我的 MVC 控制器操作中,在这种情况下,列表按预期有两个成员,但数据全为空在属性中
accessLevels[0][Id] 1
accessLevels[0][AccessLevelDesc] TEST222
accessLevels[1][Id] 2
accessLevels[1][AccessLevelDesc] TEST222
If I pass in a single object to my controller it works just fine, but I cant seem to figure out the proper way to post an array of objects to my controller from an obervablearray back to a POCO entity.
如果我将单个对象传递给我的控制器,它就可以正常工作,但我似乎无法找出将对象数组从 obervablearray 返回到 POCO 实体的正确方法。
采纳答案by Darin Dimitrov
Try sending it as a JSON request by specifying the correct request Content-Type header:
尝试通过指定正确的请求 Content-Type 标头将其作为 JSON 请求发送:
updateAccessLevels = function () {
$.ajax({
url: '/DataService/UpdateAccessLevels',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: ko.toJSON(viewModel.AccessLevels),
success: function(status) {
alert(status);
}
});
};