在MVC调用操作方法与jQuery和参数不工作

问题描述:

我试图调用使用jQuery MVC应用程序的操作方法。基本上我想要的是需要几个输入字段的值,并通过点击一个按钮,通过输入字段的值作为参数调用操作方法。但我只得到了数参数的值,而不是年的参数。

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.

    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.

下面是输入字段:

<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" />

和操作方法的开头:

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

我知道这看起来像一个奇怪的事只是场的结合,而不是,但原因是,这些输入字段和它们的值是不是这种观点的主要目的。他们只是有这个时间表应用程序,选择一个星期。再说,我要更换一个jQuery日历最终输入字段,所以我还要做这样的事情。

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.

那么,要做到这一点最简单的方法,为什么是不是因为它的工作是什么?

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

我通常使用$不用彷徨方法的jQuery的第二个参数输入URL参数。这里是一个岗位(asp.net的MVC 1,但仍然是一个有效的例子):

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):

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