英文:
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 "{order_id}"
@model MyNameSpace.Pages.OrderSheet.Approve.GeneralModel
...
<form id="form_update" method="post" /> @*used for other post methods*@
...
<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);
}
}
答案1
得分: 0
好的。我刚刚发现我的代码中有一个错误。看起来表单不允许使用 />
来关闭标签(我在 form_update 上使用了这个方式)。所以在我改成使用 </form>
后,现在它正常工作了。
<form id="form_update" method="post"></form>
英文:
OK. I just found a mistake on my code. It seems that form doesn't allow closing tag with />
(which I did on form_update). So after I changed to use </form>
instead, now it works properly.
<form id="form_update" method="post"></form>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论