c# - 如何在 ASP.NET MVC Core 2.2 中使用 @Url.Action 传递文本框值
问题描述
在我看来,我有以下代码:
<input type="text" id="createdDate" placeholder="dd/mm/yyyy" />
<a href="@Url.Action("GetRoomAccessHistory")">Download</a>
在我的控制中,我有以下代码:
[HttpGet]
public async Task<IActionResult> GetRoomAccessHistory(DateTime createdDate)
{
// TO DO
}
在这种特殊情况下,我想将文本框内的 createdDate 值 (createdDate) 传递给我的 Url.Action(...),因此它可以作为我的 URL 中的 queryString 传递。此操作作为 GET 请求调用,在 GetRoomAccessHistory 控制方法中,我应该获取我的 createdDate。
谢谢你。
附言
我认为解决方案应该是这样的:
<a href="@Url.Action("GetRoomAccessHistory", "Files", new { createdDate = ??? })" >Download</a>
解决方案
我有一个可能的答案:
<form method="post" enctype="multipart/form-data" asp-action="GetRoomAccessHistory" id="formGetRoomAccessHistory">
...
<button type="button" id="downloadRoomAccessHistory"</button>
</form>
<script>
var form = document.getElementById("formGetRoomAccessHistory");
document.getElementById("downloadRoomAccessHistory").addEventListener("click", function () {
form.submit();
});
</script>
这正是我想要的并且它有效,但我试图找到一个更好的解决方案,因为我在 ASP.NET MVC 方面的经验很低。