我基本上需要捕获插入的行的ID,并将其返回给angular应用程序。下面的代码来自asp.net Web API应用程序。我已经声明了一个整数数组
int [] documentIds = null。
mgrDocument包含
执行后的IDmgrDocument = mgrDocumentService.Create(mgrDocument);
如何将该值添加到整数数组并通过webapi返回。我有一个foreach循环,因此没有索引,否则我会做完
documentIds [index] = mgrDocument.ID;
一旦检索到所有ID,我需要将其发送给客户端以查询这些ID。然后,我该如何返回这些ID。
网络API
[HttpPost]
[SkipTokenAuthorization]
[Route("api/documentupload/create")]
public HttpResponseMessage Create(DocumentUploadCreateViewModel model)
{
HttpResponseMessage response;
var mgrStrategyDocument = new MANAGERSTRATEGY_DOCUMENT();
var mgrDocumentService = GetService<DOCUMENT>();
var mgrStrategyDocumentService = GetService<MANAGERSTRATEGY_DOCUMENT>();
int[] documentIds = null;
if (model != null)
{
foreach (var obj in model.DocumentDetails)
{
var mgrDocument = new DOCUMENT
{
DOCUMENT_TYPE_ID = obj.DocumentTypeId,
DOCUMENT_DATE = obj.DocumentDate,
NAME = obj.Name,
DOCUMENT_CONTENT = convertByteArray(obj.file),
EXTENSION = GetFileExtension(obj.Name)
};
mgrDocument = mgrDocumentService.Create(mgrDocument);
documentIds[obj] = mgrDocument.ID;
var mangerStrategyDocumentService = GetService<MANAGERSTRATEGY_DOCUMENT>();
var mgrStrategyDocument1 = new MANAGERSTRATEGY_DOCUMENT()
{
DOCUMENT_ID = mgrDocument.ID,
MANAGERSTRATEGY_ID = model.ManagerStrategyId
};
mgrStrategyDocument = mgrStrategyDocumentService.Create(mgrStrategyDocument1);
}
return response = Request.CreateResponse(HttpStatusCode.OK, mgrStrategyDocument);
}
return null;
}
这是MANAGERSTRATEGY_DOCUMENT的样子
{Permal.BSG.Manager.Core.Model.MANAGERSTRATEGY_DOCUMENT}
CREATED_BY_ID: 0
DATE_CREATED: {07/07/2019 08:01:17}
DOCUMENT: {Permal.BSG.Manager.Core.Model.DOCUMENT}
DOCUMENT_ID: 590834
ID: 186423
LAST_MODIFIED: {07/07/2019 08:01:17}
LAST_MODIFIED_BY_ID: 0
MANAGERSTRATEGY_ID: 7079
ManagerStrategy: null
答案 0 :(得分:0)
我已经通过List的Implementint返回类型解决了它
[HttpPost]
[SkipTokenAuthorization]
[Route("api/documentupload/create")]
public List<int> Create(DocumentUploadCreateViewModel model)
{
HttpResponseMessage response;
var mgrStrategyDocument = new MANAGERSTRATEGY_DOCUMENT();
var mgrDocumentService = GetService<DOCUMENT>();
var mgrStrategyDocumentService = GetService<MANAGERSTRATEGY_DOCUMENT>();
List<int> documentIds = new List<int>();
if (model != null)
{
foreach (var obj in model.DocumentDetails)
{
var mgrDocument = new DOCUMENT
{
DOCUMENT_TYPE_ID = obj.DocumentTypeId,
DOCUMENT_DATE = obj.DocumentDate,
NAME = obj.Name,
DOCUMENT_CONTENT = convertByteArray(obj.file),
EXTENSION = GetFileExtension(obj.Name)
};
mgrDocument = mgrDocumentService.Create(mgrDocument);
documentIds.Add(mgrDocument.ID);
var mangerStrategyDocumentService = GetService<MANAGERSTRATEGY_DOCUMENT>();
var mgrStrategyDocument1 = new MANAGERSTRATEGY_DOCUMENT()
{
DOCUMENT_ID = mgrDocument.ID,
MANAGERSTRATEGY_ID = model.ManagerStrategyId
};
mgrStrategyDocument = mgrStrategyDocumentService.Create(mgrStrategyDocument1);
}
return documentIds;
//return response = Request.CreateResponse(HttpStatusCode.OK, mgrStrategyDocument);
}
return null;
}