繁体   English   中英

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

[英]MVC3 Controller not receiving any parameter values

使用JQuery,我将值传递给控制器​​中的操作。 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);
                }
            });

在MVC3控制器中,我有以下操作:

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

我不知道为什么两个都为空? 请指导

MVC可能需要JSON字符串。 尝试将其用作数据

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);
                }
            });

尝试这种方式。

如果您将其更改为“ POST”请求,则它应该可以工作。

但是,您似乎实际上是在尝试从服务器“获取”数据,这些数据实际上应该在您的URL中进行编码,例如mysite.com/Customer/{customer_id}/Product/{product_id}。 在这种情况下,您可能需要更改路由规则。

我只是做了“文件->新建项目”,并添加了一个控制器,并直接尝试使用:

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);
        }
    });

它可以很好地绑定值,因此您可能想抓住提琴手或类似的东西,并确保将值实际发送到服务器。

暂无
暂无

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

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