Persisting form data on view after file upload post from same view
I have a view to edit some document details and upload two related images. I want this view to post to two different actions on my controller, one for saving all the form fields and one to do the file upload. I have one form around the main form and a multipart form around the file uplaod input
<% using (Html.BeginForm("Edit", "Document", FormMethod.Post,
new { enctype = "multipart/form-data" })) { %>
<% =Html.TextBoxFor(model => model.Document.Title) %>
<%= Html.TextBoxFor(model => model.Document.Description) %>
//and then another multipart form around the file upload part like this
<% using(Html.BeginForm("FileUpload", "Document", FormMethod.Post, new { enctype = "multipart/form-data" }))
<input type="file" id="fileUpload" name="iamge" size="30" />
<input type="submit" value="Upload" />
<% { %>
<input type="submit" value="Save" />
<% } %>
The file upload is happening correctly on posting the inner form but I want the controller to return the compelte model to the main view again with all the form fields persisited. The view is strongly typed so I thought I would post the whole model to the file upload action using the inner form. Can I get away with having just one multipart form that will post 开发者_开发知识库the files and the form data regardless of what input element is clicked? Is this good practice. At the end of the day all I want is to make sure all my form data is persisted on the Edit form after my file upload.
How can I pass the model data to the file upload action from the inner form?
You don't need the inner form I don't think. You can just have the outer form, which should parse in your model if you set up the action arguments. You could then have a different logic path in terms of, for instance, persisting other model elements depending on whether any files had been posted.
You would then be able to pass the model that had been posted back, back into the view.
精彩评论