使用 jQuery 和参数在 MVC 中调用操作方法不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4542333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:26:14  来源:igfitidea点击:

Calling action method in MVC with jQuery and parameters not working

asp.netjqueryasp.net-mvc-2

提问by Anders

I'm trying to call an action method in an MVC application using jQuery. Basically what I want is to take the value of a couple of input fields and call the action method by clicking a button, passing the values of the input fields as parameters. But I only get the value of the "number" parameter, not the "year" parameter.

我正在尝试使用 jQuery 在 MVC 应用程序中调用操作方法。基本上我想要的是获取几个输入字段的值并通过单击按钮调用操作方法,将输入字段的值作为参数传递。但是我只得到“number”参数的值,而不是“year”参数的值。

    function selectWeek() {
        $('#selectWeekButton').click(function (event) {
            var number = $("#selectWeekId").val();
            var year = $("#selectYearId").val();
            var actionUrl = '<%: Url.Action("Edit", new { number="WEEKPLACEHOLDER", year="YEARPLACEHOLDER" }) %>'

            var yearUrl = actionUrl.replace('YEARPLACEHOLDER', year);

            var url = yearUrl.replace('WEEKPLACEHOLDER', number);
            alert(url);
            $.get(url, function (data) {
                alert('Test');
            });
        });
    }

I checked the url with an alert, as you can see, and it seems to contain both values fine. But when I check the value of the year parameter in the action method it is null.

如您所见,我使用警报检查了 url,它似乎包含两个值都很好。但是当我在 action 方法中检查 year 参数的值时,它为空。

Here are the input fields:

以下是输入字段:

<span>Vecka: </span>
        <input type="text" id="selectWeekId" />
        <span>?r: </span>
        <input type="text" id="selectYearId" />
        <input type="button" value="V?lj vecka" id="selectWeekButton" />

And the beginning of the action method:

以及动作方法的开头:

public ActionResult Edit(string number, string year) 
//etc...

I know that this looks like a strange thing to do instead of just binding fields, but the reason is that these input fields and their values is not the main purpose of this View. They're just there to select another week in this timesheet application. And besides, I'm going to replace the input fields with a jQuery calendar eventually, so I will still have to do something like this.

我知道这看起来很奇怪,而不仅仅是绑定字段,但原因是这些输入字段及其值不是此视图的主要目的。他们只是在此时间表应用程序中选择另一个星期。此外,我最终将用 jQuery 日历替换输入字段,所以我仍然需要做这样的事情。

So what's the easiest way to do this, and why isn't it working as it is?

那么最简单的方法是什么,为什么它不能正常工作?

回答by rcravens

I usually use the jQuery second parameter of the $.get method to enter the URL parameters. Here is a post (asp.net mvc 1 but still a valid example):

我通常使用 $.get 方法的 jQuery 第二个参数来输入 URL 参数。这是一篇文章(asp.net mvc 1 但仍然是一个有效的例子):

http://blog.bobcravens.com/2009/11/ajax-calls-to-asp-net-mvc-action-methods-using-jquery/

http://blog.bobcravens.com/2009/11/ajax-calls-to-asp-net-mvc-action-methods-using-jquery/

回答by Darin Dimitrov

Try like this:

像这样尝试:

var number = $('#selectWeekId').val();
var year = $('#selectYearId').val();
var url = '<%: Url.Action("Edit") %>';
$.get(url, { number: number, year: year }, function (data) {
    alert('Test');
});