将 HTML 按钮转换为 ActionLink
-
12-09-2019 - |
题
我有一个 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" />
谢谢
解决方案
它不应该是那么难...但我认为你的意思是Html。链接,因为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
公共静串链接(这HtmlHelper htmlHelper,串linkText,串href,目htmlAttributes) {
TagBuilder tagBuilder=new TagBuilder("a"){InnerHtml=linkText;}
tagBuilder.MergeAttributes(htmlAttributes);
tagBuilder.MergeAttributes("href",href);
回tagBuilder.ToString(TagRenderMode.正常);
}
不要忘记 <%@进Namespace="xxxxxx"%>上查看,以便以使用扩展的方法。
其他提示
如果没有 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 会太混乱。
编辑2:
锚文本在标签内定义。我总是混淆这一点。似乎 targetSource() 是错的 也。尝试重写它:event.targetSource()=>event.target。