Home >Backend Development >C++ >How to Append a Model to FormData and Receive it as a Model in an MVC Controller?
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
<code class="language-javascript">var formdata = new FormData($('form').get(0));</code>
Receive the model in the controller
<code class="language-javascript">$.ajax({ url: '@Url.Action("YourActionName", "YourControllerName")', type: 'POST', data: formdata, processData: false, contentType: false, });</code>
or, if your model does not include httppostedFilebase, consider the following modification:
<code class="language-csharp">[HttpPost] public ActionResult YourActionName(YourModelType model) { }</code>
Including additional attributes
<code class="language-csharp">[HttpPost] public ActionResult YourActionName(YourModelType model, HttpPostedFileBase myImage) { }</code>
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.
<code class="language-javascript">formdata.append('someProperty', 'SomeValue');</code>
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!