I want to post files and other data in one request using axios and asp.net mvc
javascript:
var otherData = {
Id: 123,
Name: "Sam"
};
var formData = new FormData();
for (var i = 0; i < files.length; i++) {
formData.append("files", files[i]);
}
// I have tried these cases
// case 1:
// formData.append("request", otherData);
// case 2:
// formData.append("Id", otherData.Id);
// formData.append("Name", otherData.Name);
axios.post("/../MyAction", formData, { headers: {"Content-Type": "multipart/form-data"} })
.then(...);
Action:
public ActionResult MyAction(IEnumerable<HttpPostedFileBase> files, MyModel request)
{
}
public class MyModel
{
public int Id {get; set;}
public string Name {get; set;}
}
The files received, but the other data not
How should I adjust my code to let it working?
By the way, can I let the HttpPostedFileBase in the model like:
public class MyModel
{
public int Id {get; set;}
public string Name {get; set;}
public IEnumerable<HttpPostedFileBase> Files {get; set;}
}