i created API for attachments it should receive model info with files if i send the files the model is null if i did not send files the model data show
Model
public class AttachmentFilesViewModel
{
public long Id { get; set; }
public string FileName { get; set; }
public string FileExt { get; set; }
public IFormFile files { get; set; }
public AttachmentViewModel AttachmentViewModel {get;set; }
}
Model 2
public class AttachmentViewModel
{
public long Id { get; set; }
public string Type { get; set; }
public long TypeID { get; set; }
public string FileDesc { get; set; }
public List<AttachmentFilesViewModel> attachmentFiles {get;set; }
}
controller get files from view correct here i build my Viewmodel from the files the files is correct
[HttpPost]
public async Task<IActionResult> Create(AttachmentViewModel attachment, IFormFile[] Files)
{
LawyerAPI lawyerAPI = new LawyerAPI();
HttpClient httpClient = lawyerAPI.InitializeClient();
if (Files != null && Files.Count() > 0)
{
attachment.attachmentFiles = new List<AttachmentFilesViewModel>();
foreach (var item in Files)
{
AttachmentFilesViewModel att = new AttachmentFilesViewModel();
att.FileExt = Path.GetExtension(item.FileName);
att.FileName = item.FileName;
att.files = item;
attachment.attachmentFiles.Add(att);
}
}
HttpResponseMessage res = await httpClient.PostAsJsonAsync("api/nofactory/createattachment", attachment);
List<AttachmentViewModel> model = new List<AttachmentViewModel>();
model.Add(attachment);
return View("index", model);
}
i also tried to manually serialize
API here i receive null if i add the file to the model if i comment the above foreach and the file list is empty i receive the correct model i also tried [FromBody]
[Route("createattachment")]
// POST api/values
public IActionResult Post([FromForm] AttachmentViewModel attachment)
{}
how to get the files to the API Thanks
Edit
View Code
@using (Html.BeginForm(null, null, FormMethod.Post, new { @id ="frmAddAttachment" ,enctype="multipart/form-data"})){@Html.HiddenFor(model => model.Type)@Html.HiddenFor(model => model.TypeID)<div class="row">
<div class="card">
<div class="card-content">
<div class="form-horizontal">
<div class="col-md-10 col-sm-12 col-xs-12">
<div class="form-group">
@Html.TextBoxFor(b => b.FileDesc})
</div>
</div>
<div class="col-md-10 col-sm-12 col-xs-12">
<input type="file" name="Files" multiple="multiple" />
<button type="submit" class="btn btn-success"> Save</button>
</div>
</div>
</div>
</div>
I think the view is OK i do get the files in the controller but the problem in sending them to the API