我正在做我的第一个使用关系的简单项目,一个非常简单的清单应用程序。我试图保存带有标题的ItemList,它包含我正在使用不同模型的项目集合(项目名称和项目数量)。当我尝试从我的表来发表,我得到以下的ModelState错误:如何发布对象列表?
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'BringIt.Models.Item' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. ↵Path 'item', line 1, position 46." Name
我试图改变我的虚拟机和模型的类型,但不能让过去这个错误。这里是我现有的代码:
型号:
namespace BringIt.Models {
public class ItemList {
public int Id { get; set; }
public string Title { get; set; }
public DateTime EventDate { get; set; }
public ICollection<Item> Items { get; set; }
}
}
namespace BringIt.Models {
public class Item {
public int Id { get; set; }
public string ItemName { get; set; }
public int ItemQty { get; set; }
public string Person { get; set; }
public ItemList ItemList { get; set; }
}
}
我的VM
namespace BringIt.ViewModels {
public class AddItemsVM {
public Item item { get; set; }
public ItemList itemList { get; set; }
}
}
客户端控制器
export class CreateItemListController {
public itemList = null;
public item;
public items = [];
public addNew() {
var item = {};
this.items.push(item);
}
public save() {
this.itemListService.save(this.itemList, this.items).then(() => { this.$location.path('/') });
}
constructor(
private itemListService: MyApp.Services.ItemListService,
private $location: angular.ILocationService
) {
服务器端控制器
namespace BringIt.API
{
public class ItemListController : ApiController {
private IEFRepository _repo;
public ItemListController(IEFRepository repo) {
this._repo = repo;
}
public IHttpActionResult Post(AddItemsVM data) {
if (!ModelState.IsValid) {
return BadRequest(this.ModelState);
}
var itemList = data.itemList;
var item = data.item;
_repo.SaveItem(itemList, item);
return Created("", itemList);
}
}
}
回购
public void SaveItem(ItemList listToSave, Item items) {
if (listToSave.Id == 0) {
listToSave.EventDate = DateTime.Now;
_db.ItemLists.Add(listToSave);
_db.Items.Add(items);
_db.SaveChanges();
} else {
var original = _db.ItemLists.Find(listToSave.Id);
original.Title = listToSave.Title;
original.EventDate = listToSave.EventDate;
original.Items = listToSave.Items;
_db.SaveChanges();
}
}
}
客户端服务 命名空间MyApp.Services {
export class ItemListService {
private ItemListResource;
public save(itemList, items) {
debugger;
var data: any = {}
data.itemList = itemList;
data.item = items;
return this.ItemListResource.save(data).$promise;
}
constructor($resource: angular.resource.IResourceService) {
this.ItemListResource = $resource('/api/itemList/:id');
}
}
angular.module( 'MyApp的')。服务( 'itemListService',ItemListService );
感谢您的帮助。我一直在这里呆了太多时间,无法破解它。
如果您知道它的反序列化问题,您应该发布html/javascript代替回购。你的模型绑定器无法正确绑定,因为对于项目中的每个属性,你需要在json中有一个对应的属性,除非它的不需要的 –
你的控制器操作没有接受任何参数,你在哪里得到this.itemList等'publicUser(){ (this.itemList,this.items).then(()=> {this。$ location.path('/')}); }' –
使用杰克逊JavaScript将使您的生活更轻松。它是为这种情况而建造的。 – Aizen