我有一个 ASP.Net MVC 应用程序。我正在尝试将数据发布到操作,然后渲染输出。我有一个可以查看产品的页面,因此 www.mysite.com/product/details/1 显示了该产品。在此页面上,我通过 RenderAction() 渲染部分 View ,其中包含一些定价逻辑。我希望用户选择一些选项,然后让 jquery 发布选项以获得新价格。
但是,当我打电话时
$.post({
url : "price/getprice",
data : {"options" : chosenOptions},
success : function(data) {
$("#price").text(data);
}
});
该网址正在发布到不存在的 www.mysite.com/product/details/price/getprice
。我尝试过发布到“~/price/getprice/”,但它做了同样的事情。为什么它不会转到我的 PriceController 的 GetPrice(FormCollection form) 操作?
这应该发布到www.mysite.com/price/getprice
。当我查看 Firebug 的“网络”选项卡时,它说它正在发布以下内容:
http://localhost:42427/Product/Details/%5Bobject%20Object%5D
如果我查看 Firebug 中的响应,应用程序会引发异常:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'PrintPlaceWebsite.Controllers.ProductController'.
但我并不想发布到该位置,所以...呃。
最佳答案
您指定了一个相对 URL,因此如果此代码位于网站 www.mysite.com/a/b/c
上,则该 URL 将指向 /a/b/price/获取价格
。
您可能想要指定绝对 URL:
$.post({
url : "/price/getprice",
// ^-- leading slash
data : {"options" : chosenOptions},
success : function(data) {
$("#price").text(data);
}
});
关于jQuery 将数据发布到错误的相对 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3312724/