将 HTML 按钮转换为 ActionLink
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有 JavaScript,按钮就无法像链接一样工作。
一般来说 - 这是一个不好的做法(搜索引擎无法正确索引您的页面等)。
我建议您使用锚标记并使它们看起来像按钮。
但如果您确实需要它 - 这个文章给出了答案。
编辑:
抱歉。我的回答有点太快了。
这不完全是你要问的(不涉及 HtmlHelper),但这就是我解决这个问题的方法:
在视图中我将定义锚点(没有 href 的锚点确实通过 W3 验证):
在外部 javascript 文件中:
在 css 中:
我相信在 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):
in external javascript file:
in css:
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.
它不应该那么难......但我认为你的意思是 Html.Link 因为 ActionLink 意味着你需要从路由表生成链接。
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.
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.