在一个视图上使用 2 个 ASP.NET MVC 表单保持表单状态
所以我有一个表单,可以收集一些数据(包括来自所见即所得框的一些 HTML)并将其保存到数据库中...
我现在需要能够让用户从本地文件中获取所见即所得框的内容。在正确提交表单之前,用户需要能够看到所见即所得框中的内容。
我尝试添加第二个表单作为上传程序,并在控制器操作中抓取文件内容,将其添加到模型中,然后重定向回同一视图,但我不知道如何保留已保存的任何数据输入表格 1 的字段,以便...
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Create a New Mailing here:</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Mailing.Sender) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Mailing.Sender)%>
<%: Html.ValidationMessageFor(model => model.Mailing.Sender)%>
</div>
<div class="editor-field">
<%: Html.TextAreaFor(model => model.Mailing.Body, new { @class = "body-editor", @id = "body-editor" })%>
<%: Html.ValidationMessageFor(model => model.Mailing.Body)%>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.Mailing.Region, Model.Regions)%>
<%: Html.ValidationMessageFor(model => model.Mailing.Region)%>
</div>
<p>
<input type="submit" value="Create" id="submitMailing"/>
</p>
</fieldset>
<% } %>
<% using (Html.BeginForm("Upload", "Mail", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Load HTML from a file: </legend>
<input type="file" id="htmlFile" name="htmlFile" class="upload" />
<input type="submit" value="Engetenate" id="addContent"/>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
任何关于
[HttpPost]
public ActionResult Upload(MailingViewModel m, HttpPostedFileBase htmlFile)
{
TryUpdateModel(m);
var reader = new StreamReader(htmlFile.InputStream);
m.Mailing.Body = reader.ReadToEnd();
return View("CreateMailing",m);
}
我所缺少的东西或更好的方法的建议都会很棒......
So I've got a form that gathers some data (including some HTML from a WYSIWYG box) and saves it to a database...
I now need to be able to let the user grab content for the WYSIWYG box from a local file. The user needs to be able to see the content in the WYSIWYG box before submitting the form proper.
I've tried adding a second form that acts as an uploader and in the controller action grabbing the file content, adding it to the model and then redirecting back to the same view but I don't know how to keep any data that has been entered in the fields of form 1 so...
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Create a New Mailing here:</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Mailing.Sender) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Mailing.Sender)%>
<%: Html.ValidationMessageFor(model => model.Mailing.Sender)%>
</div>
<div class="editor-field">
<%: Html.TextAreaFor(model => model.Mailing.Body, new { @class = "body-editor", @id = "body-editor" })%>
<%: Html.ValidationMessageFor(model => model.Mailing.Body)%>
</div>
<div class="editor-field">
<%: Html.DropDownListFor(model => model.Mailing.Region, Model.Regions)%>
<%: Html.ValidationMessageFor(model => model.Mailing.Region)%>
</div>
<p>
<input type="submit" value="Create" id="submitMailing"/>
</p>
</fieldset>
<% } %>
<% using (Html.BeginForm("Upload", "Mail", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Load HTML from a file: </legend>
<input type="file" id="htmlFile" name="htmlFile" class="upload" />
<input type="submit" value="Engetenate" id="addContent"/>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
and
[HttpPost]
public ActionResult Upload(MailingViewModel m, HttpPostedFileBase htmlFile)
{
TryUpdateModel(m);
var reader = new StreamReader(htmlFile.InputStream);
m.Mailing.Body = reader.ReadToEnd();
return View("CreateMailing",m);
}
Any advice on what I'm missing or a better way to do it would be brilliant...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决此问题的一种方法是在用户选择文件后立即在后台上传文件,并在上传到服务器后将其临时存储。然后你可以使用 AJAX 来显示预览。至于后台上传文件,你可以看看 jquery form 插件 通过生成隐藏的 iframe 支持 AJAX 文件上传。由于这种 AJAX 模拟上传,表单的其他字段不会被修改,页面也不会重新加载,因此用户输入的值将被保留。
您还可以查看jQuery 文件上传演示。
One way to tackle this problem would be to upload the file in the background as soon as the user selects it and once uploaded on the server store it temporarily. Then you could use AJAX to show the preview. As far as uploading the file in the background is concerned, well, you may take a look at the jquery form plugin which supports AJAX file uploads by generating a hidden iframe. And because of this AJAX simulated upload the other fields of the form won't be modified nor the page reloaded so values entered by the user will be preserved.
You may also take a look at this jQuery File Upload Demo.