Jquery 使用Ajax防止回发的控制器的HTML.ActionLink
在我看来,这里是ASP MVC代码。它触发控制器并传递assetId和relationshipContext:Jquery 使用Ajax防止回发的控制器的HTML.ActionLink,jquery,ajax,asp.net-mvc,html.actionlink,Jquery,Ajax,Asp.net Mvc,Html.actionlink,在我看来,这里是ASP MVC代码。它触发控制器并传递assetId和relationshipContext: @Html.ActionLink("Archive", "Archive", new { assetId = Model.ID, relationshipContext = assetTypeRelation.RelationshipContextID }, new { @class = "btn btn-mini btn-warning", id = "btnArchive" })
@Html.ActionLink("Archive", "Archive", new { assetId = Model.ID, relationshipContext = assetTypeRelation.RelationshipContextID }, new { @class = "btn btn-mini btn-warning", id = "btnArchive" })
我想在这个HTML.ActionLink中使用Ajax,但我有点困惑。以下是我开始使用的jQuery。基本上,我只需要这个actionlink将assetId和relationshipContext传递给assetcontroller中的存档方法
$('#btnArchive').click(function(){
$.ajax({
url: '@Url.Action("Archive", "Archive")',
type: 'POST',
dataType: 'json',
data: {
assetId: $(this).attr("assetId"),
relationshipContext: $(this).attr("relationshipContext"),
},
success: function(){
alert("success")
},
error: function () {
alert("error")
},
});
请访问@Ajax.ActionLink
您的AssetController操作方法是什么样的?我应该看起来像:
[HttpPost]
public ActionResult Archive(int assetId, string relationshipContext)
{
//do some work
return Content("true");
}
此外,您在@Url.Action调用中调用了错误的控制器。您的视图代码可能需要更改为
$('#btnArchive').click(function(){
$.ajax({
url: '@Url.Action("Archive", "Asset")',
type: 'POST',
dataType: 'json',
data: {
assetId: $(this).attr("assetId"),
relationshipContext: $(this).attr("relationshipContext"),
},
success: function(data){
alert(data)
},
error: function () {
alert("error")
},
});
我不认识上面的那种语言。我在谷歌上搜索了一下,发现它是asp.net的一部分,特别是mvc部分。我添加了asp.net-mvc标记,但您可能需要对代码添加一些解释。您希望混合服务器端代码和客户端代码。感谢添加附加标记,我更新了问题的主体。