简体   繁体   English

在一个视图上使用2个ASP.NET MVC表单保持表单状态

[英]Keeping form state with 2 ASP.NET MVC forms on one view

So I've got a form that gathers some data (including some HTML from a WYSIWYG box) and saves it to a database... 因此,我有一个收集一些数据(包括来自“所见即所得”框中的一些HTML)并将其保存到数据库中的表单...

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... 我尝试添加第二个表单作为上传器,并在控制器操作中抓取文件内容,将其添加到模型中,然后重定向回相同的视图,但是我不知道如何保留已保存的任何数据。在表格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>

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... 关于我所缺少的任何建议或更好的解决方法都是很棒的...

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. 然后,您可以使用AJAX显示预览。 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. 至于在后台上传文件,那么,您可以看一下jquery表单插件 ,该插件通过生成隐藏的iframe支持AJAX文件上传 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. 并且由于进行了此AJAX模拟上传,因此不会修改表单的其他字段,也不会重新加载页面,因此将保留用户输入的值。

You may also take a look at this jQuery File Upload Demo . 您还可以查看此jQuery File Upload Demo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM