从同一视图上传文件后在视图上保留表单数据
我希望编辑一些文档详细信息并上传两个相关图像。我希望此视图发布到我的控制器上的两个不同操作,一个用于保存所有表单字段,一个用于执行文件上传。 我在主表单周围有一个表单,在文件上传输入周围有一个多部分表单
<% 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" />
<% } %>
文件上传在发布内部表单时正确发生,但我希望控制器再次将完整模型返回到主视图,并保留所有表单字段。该视图是强类型的,所以我想我应该使用内部表单将整个模型发布到文件上传操作。我可以只使用一个多部分表单来发布文件和表单数据,而不管单击哪个输入元素吗?这是好的做法吗?归根结底,我想要的只是确保文件上传后所有表单数据都保留在编辑表单上。
如何将模型数据从内部表单传递到文件上传操作?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不需要内在形式。您可以只使用外部形式,如果您设置了操作参数,它应该在您的模型中进行解析。然后,您可以拥有不同的逻辑路径,例如,根据是否已发布任何文件来保留其他模型元素。
然后,您就可以将已发回的模型传递回视图中。
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.