gpt4 book ai didi

javascript - 按钮单击事件未将输入字符串传递给 MVC Controller 方法

转载 作者:行者123 更新时间:2023-12-02 16:46:07 25 4
gpt4 key购买 nike

我有以下 View HTML

<input id="txt" type="text">
<button class="btn btn-primary" id="button">Upload</button>

和脚本

<script type="text/javascript">
$("#button").click(function () {
var txtVal = $("#txt").val();
window.location = "@Url.Action("Upload", "Tools")" + "/" + txtVal;
});
</script>

我的 Controller 方法是

[AllowAnonymous]
public async Task<ActionResult> Upload(string fileName)
{
string path = @"C:\GambitTests\blue-g-logo.jpg";
// Do stuff.
return RedirectToAction("Index", "Tools");
}

此方法触发,但我没有从输入框中获取文本,并且 fileNamenull

我在这里做错了什么,如何将文本从输入控件传递到此方法?

感谢您的宝贵时间。

最佳答案

在查询字符串中传递参数的默认语法是 ?paramName=value,因此在您的情况下,您需要将 JavaScript 更改为:

window.location = "@Url.Action("Upload", "Tools")" + "?fileName=" + txtVal;

或者,将参数名称更改为 id,它将按您的预期工作(由默认 RouteConfig 提供)。

关于javascript - 按钮单击事件未将输入字符串传递给 MVC Controller 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27107494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com