在 jquery 模式对话框中渲染时,输入类型=文本的表单不起作用

发布于 2025-01-06 16:47:40 字数 2581 浏览 0 评论 0 原文

我正在使用此网址中的弹出表单: http://nickstips.wordpress.com/2011/08/11/asp-net-mvc-ajax-dialog-form-using-jquery-ui/。它使用 load 函数在 div 内加载 jquery 对话框。

我有一个包含多个文本字段和 1 个上传文件字段的表单。该表单工作正常,并且我已成功将此表单发布到操作并恢复 HttpPostedFile 对象。

但是,当我使用加载函数在 jquery 对话框中加载相同的表单并将相同的表单发布到之前有效的相同操作时,它失败了。我不知道为什么,但 HttpPostedFile 对象始终为空。

实际代码: 使用上面指定的 DialogForm.js,我调用了辅助函数:

@Html.DialogFormButton("Add", Url.Action("Add", "X", new { id = y }), "", "targetId", Url.Action("List", "X", new { id = y }))

此代码应该在模式弹出窗口中加载视图:

$('.dialogLink').live('click', function () {
        var element = $(this);
...
$(dialogDiv).load(this.href, function () {
            $(this).dialog({
                modal: true,
                resizable: false,
                title: dialogTitle,
                buttons: {
                    "Save": function () {
                        // Manually submit the form
                        var form = $('form', this);
                        $(form).submit();
                    },
...

实际上,我调用一个视图,该视图加载一个编辑器模板。

  @using (Ajax.BeginForm("Test", "X", new { }, new AjaxOptions { }, new { enctype = "multipart/form-data" }))
        {
            @Html.EditorFor(m => Model, "editorView")
        }

编辑器视图包含多个文本框和输入类型文件元素:

 <tr>
        <td class="label">
            @Html.LabelFor(m => m.PathToAttachment)
        </td>
        <td>
            <div class="field_container">
                @Html.TextBoxFor(m => m.FileUpload, new { type = "file" })
            </div>
        </td>
    </tr>

所有元素都是模型类的成员。这是Post Add 方法的参数:

[HttpPost]
public ActionResult AddAttachment(AttachmentModel model) { 
  if (model.FileUpload.ContentLength > 0) { }
}

问题:model.FileUpload 始终为null,但其余表单元素具有值。请注意,只有当我使用 DialogForm.js 加载表单时才会发生这种情况。否则,当我在简单页面(不是弹出窗口)中加载表单时,我可以成功获取 FileUpload 值。

解决了,我发现无法提交带有文件的ajax表单,所以我别无选择,只能使用这个库:

http://code.google.com/p/ajax-file-upload-struts2/source/browse/trunk/src/main/resources/template/com/davidjc/javascript/ajaxfileupload.js? r=4

它创建一个带有表单元素的不可见 iframe 并自动发布到服务器。

谢谢
czetsuya

I'm using a popup form from this url: http://nickstips.wordpress.com/2011/08/11/asp-net-mvc-ajax-dialog-form-using-jquery-ui/. It loads a jquery dialog inside a div by using the load function.

I have a form with several text fields and 1 upload file field. This form works correctly, and I've successfully post this form to an action and recover the HttpPostedFile object.

But when I load the same form in a jquery dialog using the load function and post the same form to the same action that previously worked, it failed. I'm not sure why but the HttpPostedFile object is always null.

Actual codes:
Using the DialogForm.js specified above I called the helper function:

@Html.DialogFormButton("Add", Url.Action("Add", "X", new { id = y }), "", "targetId", Url.Action("List", "X", new { id = y }))

This code should load the view in a modal popup:

$('.dialogLink').live('click', function () {
        var element = $(this);
...
$(dialogDiv).load(this.href, function () {
            $(this).dialog({
                modal: true,
                resizable: false,
                title: dialogTitle,
                buttons: {
                    "Save": function () {
                        // Manually submit the form
                        var form = $('form', this);
                        $(form).submit();
                    },
...

Actually I call a view and the view loads an editor template.

  @using (Ajax.BeginForm("Test", "X", new { }, new AjaxOptions { }, new { enctype = "multipart/form-data" }))
        {
            @Html.EditorFor(m => Model, "editorView")
        }

The editor view contains several textboxes and the input type file element:

 <tr>
        <td class="label">
            @Html.LabelFor(m => m.PathToAttachment)
        </td>
        <td>
            <div class="field_container">
                @Html.TextBoxFor(m => m.FileUpload, new { type = "file" })
            </div>
        </td>
    </tr>

All the elements are member of a model class. Which is the parameter of the Post Add method:

[HttpPost]
public ActionResult AddAttachment(AttachmentModel model) { 
  if (model.FileUpload.ContentLength > 0) { }
}

The problem: model.FileUpload is always null, but the remaining of the form elements has value. Note that it only happens when I used the DialogForm.js in loading the form. Otherwise when I load the form in a simple page (not popup) I can successfully get the FileUpload value.

Resolved, I found out that it's not possible to submit an ajax form with file, so I got no choice but to use this library:

http://code.google.com/p/ajax-file-upload-struts2/source/browse/trunk/src/main/resources/template/com/davidjc/javascript/ajaxfileupload.js?r=4

It creates an invisible iframe with form elements and post to the server automatically.

Thanks

czetsuya

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哀由 2025-01-13 16:47:40

确保模态窗口中的

标记已正确设置 enctype="multipart/form-data"

Ensure the <form> tag in your modal window has enctype="multipart/form-data" set correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文