ASP.NET 5 MVC6中的@ Ajax.ActionLink

时间:2015-12-07 13:24:10

标签: asp.net-core asp.net-core-mvc

我正在使用ASP.NET 5 RC1。

ASP.NET 5

MVC 6 @Ajax.ActionLink中的等效内容

示例:

@Ajax.ActionLink("Show", 
             "Show", 
             null, 
             new AjaxOptions { HttpMethod = "GET", 
             InsertionMode = InsertionMode.Replace, 
             UpdateTargetId = "dialog_window_id", 
             OnComplete = "your_js_function();" })

ASP.NET 4 MVC 5中使用。

我明白了:

  

当前上下文中不存在名称“Ajax”。

ASP.NET 5

中的

更新

我知道它不会被实施。是否有人可以为我提供taghelper代码示例替代方案?

6 个答案:

答案 0 :(得分:5)

解释

我会在这里说话,说那时候它不是帮手。

它是一个组件。它不仅仅是HTML,还包括JavaScript。一旦将JavaScript绑定到组件,您使用的是哪个框架?您使用的是纯JavaScript吗?

如果此组件与纯JavaScript(无jQuery)绑定,则需要针对每个浏览器的所有当前版本,之前版本和未来版本进行更新/测试。

这就是为什么我认为它只是被构建为一个组件而不是构建到框架本身。

移动件太多,对客户端可以改变的框架/软件的依赖性太大。

解决方案

至于解决方案,我的建议是使用jQuery或类似的东西。

<强> HTML

<a class="ajaxLink" href="#" data-href="/Project" data-method="DELETE">Delete Project</a>

<强>的JavaScript

$(document).ready(function() {
    $("a.ajaxLink").on('click', function (){
        var self = this;
        $.ajax({
            type: $(this).attr('data-method'),
            url: $(this).attr('data-href')
        }).then(function() {
            // success callback
        });
    });
});

正如您所看到的,这可以很容易地成为一个简单的客户端解决方案,而不是服务器端解决方案。

我希望这可以回答你的问题并解决你的问题。

答案 1 :(得分:4)

&#13;
&#13;
 @Ajax.ActionLink(" ", "Edit", new { id = Model[i].RoleID }, new AjaxOptions { UpdateTargetId = "ze-partial-render", InsertionMode = InsertionMode.Replace, HttpMethod = "GET" }, new { @class = "glyphicon glyphicon-pencil btn-sm approved", })



<a asp-action="Edit" asp-controller="Account" asp-route-area="Global" asp-route-id="@item.RoleID " data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#ze-partial-render"  class="glyphicon glyphicon-pencil btn-sm"></a>
&#13;
&#13;
&#13;

答案 2 :(得分:3)

您可以使用带有data_ajax参数的常规html帮助程序,如下所示:

@Html.ActionLink("Link title", "Action", "Controller", null, new
{
   data_ajax = "true",
   data_ajax_method = "GET",
   data_ajax_mode = "replace",
   data_ajax_update = "#update-container"
})

答案 3 :(得分:3)

MVC 5 razor语法

@Ajax.ActionLink(" ", "Edit", new { id = Model[i].RoleID }, 
   new AjaxOptions { UpdateTargetId = "partial-render", 
     InsertionMode = InsertionMode.Replace, HttpMethod = "GET" },
     new { @class = "glyphicon glyphicon-pencil btn-sm approved", })

<a asp-action="Edit" asp-controller="Account" asp-route-area="Global"
   asp-route-id="@item.RoleID "
   data-ajax="true" data-ajax-method="GET"
   data-ajax-mode="replace"
   data-ajax-update="#partial-render"
   class="glyphicon 
   glyphicon-pencil btn-sm"></a>

答案 4 :(得分:2)

在ASP.NET 5中没有实现AjaxHelper的开放GitHub issue

从ASP.NET团队的评论中,他们似乎希望将其包含在发布版本中,但他们并没有解决这个问题。

答案 5 :(得分:0)

我发现这里的其他答案非常有帮助,所以我借鉴了他们并编写了一些帮助代码来简化过渡,同时我最终在这个项目中从 Unobtrusive AJAX 迁移。

虽然我可以创建一个像 Html.AjaxActionLink 这样的方法,但我查看了该方法所具有的愚蠢数量的重载,并说这对于一个小项目来说将是一个可维护性的混乱。因此,我创建了一个名为 AjaxOptions 的 DTO,我可以将其传递给 Html.ActionLink 方法。首先,这是 DTO...

public sealed class AjaxOptions
{
    public HttpMethod HttpMethod { get; set; } = HttpMethod.Get;
    //public InsertionMode InsertionMode { get; set; } // I left this out because my code only ever uses Replace
    public string UpdateTargetId { get; set; }
    public string OnBegin { get; set; }
    public string OnSuccess { get; set; }
    public string OnComplete { get; set; }

    public object ToAttrs(object htmlAttributes = null)
    {
        var dictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
        dictionary["data-ajax"] = "true";
        dictionary["data-ajax-method"] = HttpMethod.ToString().ToUpper();
        if (UpdateTargetId != null)
        {
            dictionary["data-ajax-mode"] = "replace";
            dictionary["data-ajax-update "] = '#' + UpdateTargetId;
        }
        if (OnBegin != null)
            dictionary["data-ajax-begin"] = OnBegin;
        if (OnSuccess != null)
            dictionary["data-ajax-success"] = OnSuccess;
        if (OnComplete != null)
            dictionary["data-ajax-complete"] = OnComplete;
        return dictionary;
    }
}

这让我可以像这样很好地调用 Html.ActionLink...

@(Html.ActionLink("Add Endorsement", "_CreateEndorsement", new { certificateId = Model.Id },
  new AjaxOptions { UpdateTargetId = "modal-content", OnSuccess = "OnEditModalOpened" }
    .ToAttrs(new { id = "create-endorsmeent-link" })))

不得不调用 .ToAttrs 可能看起来有点古怪,但我看着用动态和匿名对象做花哨的东西,我很快得出结论,这是更干净的解决方案,特别是考虑到我希望在下一个几个月来支持“正确的”AJAX 调用。

注意:重新使用此代码时,请注意硬编码选项并根据您的代码需要调整它们。我没有涵盖原始 AjaxOptions 类中存在的所有可能选项(属性)!

相关问题