[英]The Url.Action '@Url.Action(“Update”, “Admin”)'query string which is not working in .NET MVC
The query string generated by Ajax Ajax 生成的查询字符串
url:'@Url.Action("Update","Admin")'
is http://localhost:61887/Adimn/Update?id=16
which is not working but when the url is given as是
http://localhost:61887/Adimn/Update?id=16
这不起作用但是当 url 给出为
url: '/Admin/Update'
it generates它产生
http://localhost:61887/Adimn/Update/16
and it works fine.它工作正常。
Why is this happening?为什么会这样?
Here is my Script:这是我的脚本:
function EditUser(value) {
$.ajax({
url: '@Url.Action("Update", "Admin")'
url: '/Admin/Update',
data: {
id: value
}
})
.done(function (response) {
$("#Ajaxcontainer").html(response);
focusAction("Ajaxcontainer");
})
.fail(function (XMLHttpRequest, textStatus, errorThrown) {
alert("FAIL");
});
}
Here is the HTML Link:这是 HTML 链接:
<button type="button" class="btn btn-primary btn-sm" data-id="@item.ID" onclick="EditUser(@item.ID);">Edit</button>
You can pass parameters to Url.Action, for instance:您可以将参数传递给 Url.Action,例如:
Url.Action("Update", "Admin", new { id = id });
So:所以:
$.ajax({
url: '@Url.Action("Update", "Admin", new { id = 16})',
//url: '/Admin/Update/16',
contentType: "application/json",
success: function (result) {
// your code here
},
error: function (xhr, resp, text) { console.log(xhr, resp, text); }
});
This way, the requested url will be: http://localhost:61887/Adimn/Update/16
这样,请求的 url 将是:
http://localhost:61887/Adimn/Update/16
While,尽管,
$.ajax({
url: '/Admin/Update/',
type: "GET",
data: {
"id":16
},
contentType: "application/json",
success: function (result) { alert('success'); },
error: function (result) { alert('Error'); }
});
This way, the requested url will be: http://localhost:61887/Adimn/Update/?id=16
这样,请求的 url 将是:
http://localhost:61887/Adimn/Update/?id=16
The reason why you see query string at the end of the url ?id=16
is because by default you're sending Get
request by ajax.您之所以在 url
?id=16
的末尾看到查询字符串,是因为默认情况下您通过 ajax 发送Get
请求。 You may decide to use type: "POST",
in ajax call to avoid query string, and instead, have the model data bundled and sent through body of message.您可以决定在 ajax 调用中使用
type: "POST",
以避免查询字符串,而是将 model 数据捆绑并通过消息正文发送。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.