将 HTML 按钮转换为 ActionLink

发布于 2024-08-04 23:32:07 字数 397 浏览 6 评论 0原文

我有一个 html 按钮设置和功能,具有一组必需的属性,我想将其转换为基于文本的链接。另外,这样我就可以熟悉工作 html 助手和智能感知,我想看看如何将这些属性硬塞到 ActionLink 中:

<input type="button" id="RemoveRegistration_Submit<%=row.ID %>" 
value="Remove From Cart" 
onclick="$('#Step2_RemoveRegistrationForm input[name=id]').val('<%=row.ID %>'); $('#Step2_RemoveRegistrationForm').submit();" 
align="right" />

thx

I have an html button setup and functioning with a set range of required properties that I'd like to convert to a text-based link instead. Additionally, so I can familiarize myself with working html helpers and intellesense I'd like to see how to shoehorn these properties into an ActionLink:

<input type="button" id="RemoveRegistration_Submit<%=row.ID %>" 
value="Remove From Cart" 
onclick="$('#Step2_RemoveRegistrationForm input[name=id]').val('<%=row.ID %>'); $('#Step2_RemoveRegistrationForm').submit();" 
align="right" />

thx

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

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

发布评论

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

评论(2

醉酒的小男人 2024-08-11 23:32:07

如果没有 JavaScript,按钮就无法像链接一样工作。
一般来说 - 这是一个不好的做法(搜索引擎无法正确索引您的页面等)。

我建议您使用锚标记并使它们看起来像按钮。

但如果您确实需要它 - 这个文章给出了答案。

编辑:

抱歉。我的回答有点太快了。

这不完全是你要问的(不涉及 HtmlHelper),但这就是我解决这个问题的方法:

在视图中我将定义锚点(没有 href 的锚点确实通过 W3 验证):

<a id='removefromcart_<%=row.ID%>' title='Remove from cart' 
   class='remove-link' />

在外部 javascript 文件中:

   var onclick = function(event){
        event.preventDefault();
        var link = $(event.targetSource());

        //tag ids should be injected through view asp/cx
        $('#Step2_RemoveRegistrationForm input[name=id]') 
            .val(link.attr('id').split('_')[1])
    };
    $('a[id^=removefromcart]').click(onclick);  

在 css 中:

 a {cursor:pointer;} /*anchors without href by default haven't pointer*/

我相信在 HtmlHelpers 中使用 javascript 会太混乱。

EDIT2:

锚文本在标签内定义。我总是混淆这一点。看来 targetSource() 也是错误的。尝试重写它:event.targetSource()=>event.target。

Buttons can't act like links without javascript.
In general - that's a bad practice (search engines can't index your page correctly etc.).

I would recommend you to use anchor tags and make them look like a buttons.

But if you truly need it - this article provides an answer.

EDIT:

Sorry. Shot my answer little bit too fast.

This isn't exactly what you are asking (HtmlHelper is not involved) but that's how I would solve this problem:

in view i would define anchor (anchors without hrefs do pass W3 validation):

<a id='removefromcart_<%=row.ID%>' title='Remove from cart' 
   class='remove-link' />

in external javascript file:

   var onclick = function(event){
        event.preventDefault();
        var link = $(event.targetSource());

        //tag ids should be injected through view asp/cx
        $('#Step2_RemoveRegistrationForm input[name=id]') 
            .val(link.attr('id').split('_')[1])
    };
    $('a[id^=removefromcart]').click(onclick);  

in css:

 a {cursor:pointer;} /*anchors without href by default haven't pointer*/

I believe it would be too messy to poke around with javascript in HtmlHelpers.

EDIT2:

Anchor text is defined inside tags. I always confuse that. And it seems that targetSource() is wrong too. Try to rewrite it: event.targetSource()=>event.target.

贱人配狗天长地久 2024-08-11 23:32:07

它不应该那么难......但我认为你的意思是 Html.Link 因为 ActionLink 意味着你需要从路由表生成链接。

<%= Html.Link("Remove from Cart", "#", new {onclick = "#Step2_RemoveRegistrationForm input[name=id]').val('<%=row.ID %>'); $('#Step2_RemoveRegistrationForm').submit();"}) %>
  • param#1: linkText
  • param#2: href
  • param#3: htmlAttributes

    public static string Link(this HtmlHelper htmlHelper, string linkText, string href, object htmlAttributes)
    {

    TagBuilder tagBuilder = new TagBuilder("a"){ InnerHtml = linkText; }

    tagBuilder.MergeAttributes(htmlAttributes);

    tagBuilder.MergeAttributes("href", href);

    返回 tagBuilder.ToString(TagRenderMode.Normal);

    }

不要忘记 <%@ Import Namespace="xxxxxx" %>在视图上以便使用扩展方法。

It shouldn't be that hard....but I think you mean Html.Link because ActionLink mean you need to generate link from route table.

<%= Html.Link("Remove from Cart", "#", new {onclick = "#Step2_RemoveRegistrationForm input[name=id]').val('<%=row.ID %>'); $('#Step2_RemoveRegistrationForm').submit();"}) %>
  • param#1: linkText
  • param#2: href
  • param#3: htmlAttributes

    public static string Link(this HtmlHelper htmlHelper, string linkText, string href, object htmlAttributes)
    {

    TagBuilder tagBuilder = new TagBuilder("a"){ InnerHtml = linkText; }

    tagBuilder.MergeAttributes(htmlAttributes);

    tagBuilder.MergeAttributes("href", href);

    return tagBuilder.ToString(TagRenderMode.Normal);

    }

and don't forget to <%@ Import Namespace="xxxxxx" %> on the view in order to use the extension method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文