简体   繁体   English

MVC3控制器未收到任何参数值

[英]MVC3 Controller not receiving any parameter values

Using JQuery, I am passing values to an action in the controller. 使用JQuery,我将值传递给控制器​​中的操作。 customerId and productId are not null: customerId和productId不为null:

$.ajax({
                type: "GET",
                url: "Customer/Product/",
                data: { Customer: customerID, Product: productId},
                dataType: "json",
                error: function (xhr, status, error) {
                    // you may need to handle me if the json is invalid
                    // this is the ajax object
                },
                success: function (json) {
                    $("#productName").innerHTML = json;
                    alert(json);
                    alert($("#startDate").innerHTML);
                }
            });

In MVC3 controller, i have the action: 在MVC3控制器中,我有以下操作:

public ActionResult Product(string Customer, string Product)
{
//both are null
}

I don't know why both are null? 我不知道为什么两个都为空? Please guide 请指导

MVC may be expecting a JSON string. MVC可能需要JSON字符串。 Try using this as your data 尝试将其用作数据

data: JSON.stringify({ Customer: customerID, Product: productId})
$.ajax({
                type: "GET",
                url: "Customer/Product/",
                data:  "Customer="+customerID +"&Product="+productId,
                dataType: "json",
                error: function (xhr, status, error) {
                    // you may need to handle me if the json is invalid
                    // this is the ajax object
                },
                success: function (json) {
                    $("#productName").innerHTML = json;
                    alert(json);
                    alert($("#startDate").innerHTML);
                }
            });

Try this way. 尝试这种方式。

If you change it to a "POST" request it should work. 如果您将其更改为“ POST”请求,则它应该可以工作。

However it looks like you are actually trying to just "GET" data from the server which should really be encoded in your URL eg mysite.com/Customer/{customer_id}/Product/{product_id}. 但是,您似乎实际上是在尝试从服务器“获取”数据,这些数据实际上应该在您的URL中进行编码,例如mysite.com/Customer/{customer_id}/Product/{product_id}。 In this case you'll probably need to change your routing rules. 在这种情况下,您可能需要更改路由规则。

I just did "File -> New Project" and just added the one controller and tried directly using: 我只是做了“文件->新建项目”,并添加了一个控制器,并直接尝试使用:

var customerID = 42;
var productId = 4242;
$.ajax({
        type: "GET",
        url: "http://localhost:51622/Customer/Product",
        data: { Customer: customerID, Product: productId},
        dataType: "json",
        error: function (xhr, status, error) {
            // you may need to handle me if the json is invalid
            // this is the ajax object
        },
        success: function (json) {
            console.log(json);
        }
    });

It binds the values just fine, so you might want to grab fiddler or something similiar and make sure that you are actually sending values to the server. 它可以很好地绑定值,因此您可能想抓住提琴手或类似的东西,并确保将值实际发送到服务器。

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

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