Base solution for your next web application
Open Closed

Persisting data #11697


User avatar
0
alliance225 created

I have created the following entity using Power Tools And in my controller I have the code below to insert . However after the await _documentRepository.InsertAsync(document) is executed, the data is not persisted right away. It is posted only when the method has done executing. But I need to get the id of the document

[Table("Documents")] [Audited] public class Document : Entity, IMayHaveTenant { public int? TenantId { get; set; }

 [Required]
 [StringLength(DocumentConsts.MaxLibelleLength, MinimumLength = DocumentConsts.MinLibelleLength)]
 public virtual string Libelle { get; set; }

 public virtual DateTime DateDocument { get; set; }

 public virtual int DocumentTypeId { get; set; }

 [ForeignKey("DocumentTypeId")]
 public DocumentType DocumentTypeFk { get; set; }

 public virtual int? DocumentCategoryId { get; set; }

 [ForeignKey("DocumentCategoryId")]
 public DocumentCategory DocumentCategoryFk { get; set; }

}

[HttpPost] public async Task<IActionResult> Index(int Id) { // Get the Document Libelle from the request form string Libelle = Request.Form["Libelle"]; DateTime DateDocument = Convert.ToDateTime(Request.Form["DateDocument"]); int DocumentCategoryId = Convert.ToInt32(Request.Form["DocumentCategoryId"]); int DocumentTypeId = Convert.ToInt32(Request.Form["DocumentTypeId"]);

   // Get the file
   var file = Request.Form.Files.FirstOrDefault();

   // Create the Document
   var createDocumentDto = new CreateOrEditDocumentDto
   {
       Libelle = Libelle,
       DateDocument = DateDocument,
       DocumentCategoryId = DocumentCategoryId,
       DocumentTypeId = DocumentTypeId
   };
   await _documentAppService.CreateOrEdit(createDocumentDto);

   // Get the created or edited document's id
   var documentId = createDocumentDto.Id.Value;

   return RedirectToAction("Index");

}


No answer yet!