Appending and Receiving a Model in Formdata
To pass a model object as part of a formdata object and retrieve it in the controller, consider the following approach:
JavaScript:
Create a FormData object:
var formdata = new FormData($('form').get(0));
Convert the model to JSON using JSON.stringify():
let model = { EventFromDate: fromDate, EventToDate: toDate, ... }; const modelJson = JSON.stringify(model);
Append the JSON string to the formdata:
formdata.append("model", modelJson);
AJAX Call:
$.ajax({ url: '@Url.Action("YourActionName", "YourControllerName")', type: 'POST', data: formdata, processData: false, contentType: false, });
Controller:
Declare a parameter of the appropriate model type:
[HttpPost] public ActionResult YourActionName(YourModelType model) { // Your code to process the model here... }
This approach allows you to append the entire model as JSON data to the formdata and retrieve it in the controller as a model object, enabling you to work with complex models in a controller action.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3