【问题标题】:Where is "system.web.mvc.ajaxhelper" in asp.net core 1.0asp.net core 1.0 中的“system.web.mvc.ajaxhelper”在哪里
【发布时间】:2016-05-13 17:49:42
【问题描述】:

我正在“rc1-final”上试用新的 asp.net 5(现在是核心 1.0),直到 Asp.Net 4 在“system.web.mvc”上才找到旧类 AjaxHelper " dll。

是否有任何包含此类的 nuget 包?或者其他可以替代这个的?

我正在使用“DNX 452”,不要假装现在要迁移到“core 1.0”/“dnx 5”。

【问题讨论】:

标签: c# asp.net asp.net-core


【解决方案1】:

mvc ajaxhelpers 主要只是设置jquery unobtrusive ajax 使用的data-ajax-* 属性。只需在标记中添加属性,您就可以在 ASP.NET Core 中轻松、更干净地做到这一点,或者如果您想对它感兴趣,您可以实现 taghelpers。例如,在较旧的 MVC 5 中,我曾经使用 ajaxhelper 来实现自定义引导模式对话框。当我将它移植到 ASP.NET Core 时,我发现像你一样不存在 ajaxhelper,所以我实现了一个 taghelper,它添加了这样的 ajax 属性:

/// <summary>
/// this taghelper detects the bs-modal-link attribute and if found (value doesn't matter)
/// it decorates the link with the data-ajax- attributes needed to wire up the bootstrap modal
/// depends on jquery-ajax-unobtrusive and depends on cloudscribe-modaldialog-bootstrap.js
/// </summary>
[HtmlTargetElement("a", Attributes = BootstrapModalLinkAttributeName)]
public class BootstrapModalAnchorTagHelper : TagHelper
{
    private const string BootstrapModalLinkAttributeName = "bs-modal-link";

    public BootstrapModalAnchorTagHelper() : base()
    {

    }


    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        // we don't need to output this attribute it was only used for matching in razor
        TagHelperAttribute modalAttribute = null;
        output.Attributes.TryGetAttribute(BootstrapModalLinkAttributeName, out modalAttribute);
        if (modalAttribute != null) { output.Attributes.Remove(modalAttribute); }

        var dialogDivId = Guid.NewGuid().ToString();
        output.Attributes.Add("data-ajax", "true");
        output.Attributes.Add("data-ajax-begin", "prepareModalDialog('" + dialogDivId + "')");
        output.Attributes.Add("data-ajax-failure", "clearModalDialog('" + dialogDivId + "');alert('Ajax call failed')");
        output.Attributes.Add("data-ajax-method", "GET");
        output.Attributes.Add("data-ajax-mode", "replace");
        output.Attributes.Add("data-ajax-success", "openModalDialog('" + dialogDivId + "')");
        output.Attributes.Add("data-ajax-update", "#" + dialogDivId);

    }
}

【讨论】:

  • 是的,我发现这种方式是唯一的一种两种。但是,我没有从 Taghelper 继承(基于我的旧助手),而是使用 HtmlHelper 的扩展。 TagHelper 有什么好处?
  • 如果你用谷歌搜索“taghelpers 的好处”,你会发现很好的信息。主要是关于更干净的标记和更干净地将标记从 c# 与 HtmlHelpers 分开。很好的例子davepaquette.com/archive/2015/05/11/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 2017-04-15
  • 1970-01-01
相关资源
最近更新 更多