MVC3 ActionLink 有/没有图像差异

发布于 2024-12-24 20:43:28 字数 2635 浏览 0 评论 0原文

我浏览了不同的帖子,找到了一个扩展来创建一个操作链接助手,它将添加一个图像作为链接,如下所示。

public static MvcHtmlString ImageLink(this HtmlHelper helper, string actioNName, string imgUrl, string alt, object routeValues, object linkHtmlAttributes, object imageHtmlAttributes)
    {
        var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
        var url = urlHelper.Action(actioNName, routeValues);

    //Create the link
    var linkTagBuilder = new TagBuilder("a");
    linkTagBuilder.MergeAttribute("href", url);
    linkTagBuilder.MergeAttributes(new RouteValueDictionary(linkHtmlAttributes));

    //Create image
    var imageTagBuilder = new TagBuilder("img");
    imageTagBuilder.MergeAttribute("src", urlHelper.Content(imgUrl));
    imageTagBuilder.MergeAttribute("alt", urlHelper.Content(alt));
    imageTagBuilder.MergeAttributes(new RouteValueDictionary(imageHtmlAttributes));

    //Add image to link
    linkTagBuilder.InnerHtml = imageTagBuilder.ToString(TagRenderMode.SelfClosing);

    //return linkTagBuilder.ToString();
    return MvcHtmlString.Create(linkTagBuilder.ToString());
} 

它生成了我“认为我需要”的正确 HTML,但是当我运行它时出现错误,“对象不支持此属性或方法”。然而,当我只使用 MVC HtmlAction Link 创建基本上相同的东西时,它确实可以工作。我认为问题在于它是嵌套的,我必须更改 jQuery 代码才能启动对话框。这是代码。

<script type="text/javascript">

    $.ajaxSetup({ cache: false });

    $(document).ready(function () {
        $(".openDialog").live("click", function (e) {
            e.preventDefault();

            $("<div></div>")
                    .addClass("dialog")
                    .attr("id", $(this).attr("data-dialog-id"))
                    .appendTo("body")
                    .dialog({
                        title: $(this).attr("data-dialog-title"),
                        close: function () { $(this).remove() },
                        modal: true
                    })
                    .load(this.href);
        });

        $(".close").live("click", function (e) {
            e.preventDefault();
            $(this).closest(".dialog").dialog("close");
        });
    });
</script>

这是使用图像生成的 HTML,如果不使用图像,则一种有效,另一种则无效。

//Without the Image and it works fine
      <a class="openDialog" data-dialog-id="emailDialog" data-dialog-title="Contact Us" href="/Home/ContactUs">Contact Us</a>

//With the Image that produces an Error
                <a class="openDialog" data_dialog_id="emailDialog" data_dialog_title="Contact Us" href="/Home/ContactUs"><img alt="Contact us" src="Content/images/common_en/subnavi_contact.png" /></a>

任何帮助将不胜感激。提前致谢。

I went through the different posts and found an extension to create an Action Link Helper that will add an image as the link as follows.

public static MvcHtmlString ImageLink(this HtmlHelper helper, string actioNName, string imgUrl, string alt, object routeValues, object linkHtmlAttributes, object imageHtmlAttributes)
    {
        var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
        var url = urlHelper.Action(actioNName, routeValues);

    //Create the link
    var linkTagBuilder = new TagBuilder("a");
    linkTagBuilder.MergeAttribute("href", url);
    linkTagBuilder.MergeAttributes(new RouteValueDictionary(linkHtmlAttributes));

    //Create image
    var imageTagBuilder = new TagBuilder("img");
    imageTagBuilder.MergeAttribute("src", urlHelper.Content(imgUrl));
    imageTagBuilder.MergeAttribute("alt", urlHelper.Content(alt));
    imageTagBuilder.MergeAttributes(new RouteValueDictionary(imageHtmlAttributes));

    //Add image to link
    linkTagBuilder.InnerHtml = imageTagBuilder.ToString(TagRenderMode.SelfClosing);

    //return linkTagBuilder.ToString();
    return MvcHtmlString.Create(linkTagBuilder.ToString());
} 

Which produces the proper HTML that I "think I need" But I get an error when I run it, 'Object Doesnt Support this property or method'. However when I just use the MVC HtmlAction Link that creates basically the same thing it does work. I think the problem is the fact that its nested, and I would have to change the jQuery code to launch the dialog. Here is the code for that.

<script type="text/javascript">

    $.ajaxSetup({ cache: false });

    $(document).ready(function () {
        $(".openDialog").live("click", function (e) {
            e.preventDefault();

            $("<div></div>")
                    .addClass("dialog")
                    .attr("id", $(this).attr("data-dialog-id"))
                    .appendTo("body")
                    .dialog({
                        title: $(this).attr("data-dialog-title"),
                        close: function () { $(this).remove() },
                        modal: true
                    })
                    .load(this.href);
        });

        $(".close").live("click", function (e) {
            e.preventDefault();
            $(this).closest(".dialog").dialog("close");
        });
    });
</script>

This is the HTML that gets produces with the image and without, one works one doesnt.

//Without the Image and it works fine
      <a class="openDialog" data-dialog-id="emailDialog" data-dialog-title="Contact Us" href="/Home/ContactUs">Contact Us</a>

//With the Image that produces an Error
                <a class="openDialog" data_dialog_id="emailDialog" data_dialog_title="Contact Us" href="/Home/ContactUs"><img alt="Contact us" src="Content/images/common_en/subnavi_contact.png" /></a>

Any help would be appreciated. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没结果 2024-12-31 20:43:28

试试这个,确保错误发生在appendTo行上:

var newDiv = $("<div></div>");
newDiv.addClass("dialog");
newDiv.attr("id", $(this).data("dialog-id"));
newDiv.appendTo($('body')); // is error really here?
newDiv.dialog({
    title: $(this).data("dialog-title"),
    close: function () { $(this).remove() },
    modal: true
}).load(this.href);

另外,您使用的是哪个版本的jQuery?您可以使用 data() 函数代替 attr() 函数来简化代码。

评论后更新

我的错!我没有意识到:在对话框内部时, $(this) 不再引用 .openDialog 链接。试试这个:

var newDiv = $("<div></div>");
newDiv.addClass("dialog");
newDiv.attr("id", $(this).data("dialog-id"));
newDiv.appendTo($('body')); // is error really here?
var anchor = $(this);
newDiv.dialog({
    title: anchor.data("dialog-title"),
    close: function () { $(this).remove() },
    modal: true
}).load(this.href);

Try this, make sure the error is happening on the appendTo line:

var newDiv = $("<div></div>");
newDiv.addClass("dialog");
newDiv.attr("id", $(this).data("dialog-id"));
newDiv.appendTo($('body')); // is error really here?
newDiv.dialog({
    title: $(this).data("dialog-title"),
    close: function () { $(this).remove() },
    modal: true
}).load(this.href);

Also, what version of jQuery are you using? You can use the data() function instead of the attr() function to shorthand the code a bit.

Update after comment

My bad! I didn't realize: when inside of dialog, $(this) no longer refers to the .openDialog link. Try this instead:

var newDiv = $("<div></div>");
newDiv.addClass("dialog");
newDiv.attr("id", $(this).data("dialog-id"));
newDiv.appendTo($('body')); // is error really here?
var anchor = $(this);
newDiv.dialog({
    title: anchor.data("dialog-title"),
    close: function () { $(this).remove() },
    modal: true
}).load(this.href);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文