简体   繁体   English

按钮单击事件未将输入字符串传递给MVC控制器方法

[英]Button Click Event not Passing input String to MVC Controller Method

I have the following view HTML 我有以下视图HTML

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

and the script 和脚本

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

My controller method is 我的控制器方法是

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

this method fires but I am not getting the text from the input box and fileName is null . 此方法将触发,但是我没有从输入框中获取文本,并且fileNamenull

What am I doing wrong here how can I pass the text from the input control to this method? 我在做什么错,如何将文本从输入控件传递给此方法?

Thanks for your time. 谢谢你的时间。

The default syntax for passing parameters in the query string is ?paramName=value, so in your case you'd need to change your JavaScript to: 在查询字符串中传递参数的默认语法是?paramName = value,因此在您的情况下,您需要将JavaScript更改为:

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

Alternatively, change the parameter name to id and it'll work as you expect (courtesy of the default RouteConfig). 或者,将参数名称更改为id,它将按您期望的方式工作(由默认RouteConfig提供)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM