[英]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.