Razor Page – 表单未提交

huangapple go评论65阅读模式
英文:

Razor Page - Form not submitting

问题

General.cshtml

我目前正在开发 Razor Page(.NET Core 6)。我遇到的问题是表单(在这种情况下是“**form_Download**”)没有提交。奇怪的是,我在其他页面也有类似的代码,那边是可以正常工作的。我想知道为什么这个表单没有提交。以下是我的代码:

@page "{order_id}"
@model MyNameSpace.Pages.OrderSheet.Approve.GeneralModel
...
<form id="form_update" method="post"> @*用于其他的 post 方法*@</form>
...
<form id="form_Download" method="post" asp-page-handler="DownloadAttachment">
    <input id="hdn_AttachId" name="attach_id" type="hidden" />
</form>
@section Scripts {
    <script src="~/lib/DevExtreme/dx.all.js"></script>
    <script type="text/javascript">

        const formUpdate = document.querySelector("#form_update");

        $(() => {

            //...

            $('#grid_Attachment').dxDataGrid({
                dataSource: attachmentStore,
                columns: [{
                    caption: 'No',
                    dataField: 'attach_id',
                }, {
                    caption: 'Type',
                    dataField: 'type',
                }, {
                    caption: 'Name',
                    dataField: 'file_name',
                    cellTemplate(cell, info) {
                        const { data: { attach_id, file_name, file_extension } } = info.row;
                        $('<div>')
                            .html(`<a href="#" class="dx-link dx-link-edit" onclick="DownloadAttachment(${attach_id})">${file_name}${file_extension}</a>`)
                            .appendTo(cell);
                    },
                }, {
                    caption: 'Remark',
                    dataField: 'remark',
                }],
                remoteOperations: false,
                showBorders: true,
            });
        });

        //...

        function DownloadAttachment(attach_id) {
            $("#hdn_AttachId").val(attach_id);
            $("#form_Download").submit();
        }
    </script>
}

General.cshtml.cs

public class GeneralModel : PageModel
{
    //...

    public IActionResult OnPostDownloadAttachment(int order_id, int attach_id)
    {
        (byte[] bytes, string contentType, string filename) = _attachmentManager.DownloadFile(order_id, attach_id)!;
        return File(bytes, contentType, filename);
    }
}
英文:

I am currently working on Razor Page (.NET Core 6). The problem I encountered is that form ("form_Download", in this case) is not submitting. The strange thing is that I also have similar code in other pages and it works properly. I wonder why the form is not submitted. Here are my codes:

General.cshtml

@page &quot;{order_id}&quot;
@model MyNameSpace.Pages.OrderSheet.Approve.GeneralModel
...
&lt;form id=&quot;form_update&quot; method=&quot;post&quot; /&gt; @*used for other post methods*@
...
&lt;form id=&quot;form_Download&quot; method=&quot;post&quot; asp-page-handler=&quot;DownloadAttachment&quot;&gt;
&lt;input id=&quot;hdn_AttachId&quot; name=&quot;attach_id&quot; type=&quot;hidden&quot; /&gt;
&lt;/form&gt;
@section Scripts {
&lt;script src=&quot;~/lib/DevExtreme/dx.all.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
const formUpdate = document.querySelector(&quot;#form_update&quot;);
$(() =&gt; {
//...
$(&#39;#grid_Attachment&#39;).dxDataGrid({
dataSource: attachmentStore,
columns: [{
caption: &#39;No&#39;,
dataField: &#39;attach_id&#39;,
}, {
caption: &#39;Type&#39;,
dataField: &#39;type&#39;,
}, {
caption: &#39;Name&#39;,
dataField: &#39;file_name&#39;,
cellTemplate(cell, info) {
const { data: { attach_id, file_name, file_extension } } = info.row;
$(&#39;&lt;div&gt;&#39;)
.html(`&lt;a href=&quot;#&quot; class=&quot;dx-link dx-link-edit&quot; onclick=&quot;DownloadAttachment(${attach_id})&quot;&gt;${file_name}${file_extension}&lt;/a&gt;`)
.appendTo(cell);
},
}, {
caption: &#39;Remark&#39;,
dataField: &#39;remark&#39;,
}],
remoteOperations: false,
showBorders: true,
});
});
//...
function DownloadAttachment(attach_id) {
$(&quot;#hdn_AttachId&quot;).val(attach_id);
$(&quot;#form_Download&quot;).submit();
}
&lt;/script&gt;
}

General.cshtml.cs

public class GeneralModel : PageModel
{
//...
public IActionResult OnPostDownloadAttachment(int order_id, int attach_id)
{
(byte[] bytes, string contentType, string filename) = _attachmentManager.DownloadFile(order_id, attach_id)!;
return File(bytes, contentType, filename);
}
}

答案1

得分: 0

好的。我刚刚发现我的代码中有一个错误。看起来表单不允许使用 /&gt; 来关闭标签(我在 form_update 上使用了这个方式)。所以在我改成使用 &lt;/form&gt; 后,现在它正常工作了。

&lt;form id=&quot;form_update&quot; method=&quot;post&quot;&gt;&lt;/form&gt;
英文:

OK. I just found a mistake on my code. It seems that form doesn't allow closing tag with /&gt; (which I did on form_update). So after I changed to use &lt;/form&gt; instead, now it works properly.

&lt;form id=&quot;form_update&quot; method=&quot;post&quot;&gt;&lt;/form&gt;

huangapple
  • 本文由 发表于 2023年2月19日 13:59:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定