Hi,@Andrej If you want to reduce the codes in controller,you could create a new method in your controller, refactor the controller,For example: Modify
public IActionResult UploadDatabase()
{
var formAccumulator = new KeyValueAccumulator();
// Bind form data to the model
var formData = new FormData();
var formValueProvider = new FormValueProvider(
BindingSource.Form,
new FormCollection(formAccumulator.GetResults()),
CultureInfo.CurrentCulture);
var bindingSuccessful = await TryUpdateModelAsync(formData, prefix: "",
valueProvider: formValueProvider);
if (!bindingSuccessful)
{
ModelState.AddModelError(
"File",
"The request couldn't be processed (Error 5).");
// Log error
return BadRequest(ModelState);
}
return Created(nameof(StreamingController), null);return View();
}
To
public async Task<IActionResult> UploadDatabase()
{
var formAccumulator = new KeyValueAccumulator();
...........
var bindingSuccessful = await BindFormData(formAccumulator);
if (!bindingSuccessful)
{
ModelState.AddModelError("File",
"The request couldn't be processed (Error 5).");
// Log error
return BadRequest(ModelState);
}
return Created(nameof(StreamingController), null);
}
private async Task<bool> BindFormData(KeyValueAccumulator formAccumulator)
{
//Bind form data to the model
var formData = new MultipartFormDataContent();
var formValueProvider = new FormValueProvider(
BindingSource.Form,
new FormCollection(formAccumulator.GetResults()),
CultureInfo.CurrentCulture);
return await TryUpdateModelAsync(formData, prefix: "",
valueProvider: formValueProvider);
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards, Ruikai Feng