我有一个名为 InvoiceTo 的 View 和一个名为 Order 的 Controller 。使用 JQuery,默认 URL 为:url: ''
,结果如下:
locahost:端口/域/订单/InvoiceTo
如果我更改 URL:url:/Order/GetInformation
,结果如下:
locahost:端口/域/订单/InvoiceTo/Order/GetInformation
我已经尝试了很多方法来设置我的网址,但总是错误的。这是我的 JQuery:
$(document).ready(function () {
$('#InvoiceToDrop').change(function () {
var $div = $('#modalPartial');
var idcustomer = $(this).val();
$.ajax({
url: '/Order/GetInformation/' + idcustomer,
type: 'GET',
success: function (data) {
alert(JSON.stringify(data));
},
error: function (error) {
}
})
});
});
我需要做什么才能获得此 URL:
locahost:端口/域/Order/GetInformation/1
最佳答案
您可以使用UrlHelper View 中的 Url.Action
方法:
$.ajax({
url: '@Url.Action("GetInformation", "Order", new { customerId })',
...
或者
url: '@Url.Action("GetInformation", "Order")/' + idcustomer
当 View 加载时,@Url.Action
被解析并被实际值替换。
使用此方法的优点是它使用路由表而不是静态类型的 URL。
关于c# - 通过 JQuery 访问 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55835797/