This article provides a complete solution to solve the common problems that transmit the complete model set through FormData and use it as a model access in the controller.
The traditional method is to add the model object as a string to the FormData, which will cause the Request.form set in the controller to receive the "[Object Object]". In order to overcome this limit, a better method can be used:
Use FormData () serialized model
Use Ajax to release data
var formdata = new FormData($('form').get(0));
Receive the model in the controller
$.ajax({ url: '@Url.Action("YourActionName", "YourControllerName")', type: 'POST', data: formdata, processData: false, contentType: false, });
or, if your model does not include httppostedFilebase, consider the following modification:
[HttpPost] public ActionResult YourActionName(YourModelType model) { }
Including additional attributes
[HttpPost] public ActionResult YourActionName(YourModelType model, HttpPostedFileBase myImage) { }
Through these technologies, you can seamlessly add model data to FormData, so that it is convenient to transmit it as a model type in the operation method of the controller.
formdata.append('someProperty', 'SomeValue');
The above is the detailed content of How to Append a Model to FormData and Receive it as a Model in an MVC Controller?. For more information, please follow other related articles on the PHP Chinese website!