加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – ASP.NET向razor添加id ActionLink

发布时间:2020-12-16 09:12:46 所属栏目:asp.Net 来源:网络整理
导读:参见英文答案 Passing parameter to controller action from a Html.ActionLink????????????????????????????????????3个 我需要剃须刀来生成一个href链接.这段代码工作正常: @Html.ActionLink("Questions","Questions","SectionPartials") 如何将id元素设
参见英文答案 > Passing parameter to controller action from a Html.ActionLink????????????????????????????????????3个
我需要剃须刀来生成一个href链接.这段代码工作正常:

@Html.ActionLink("Questions","Questions","SectionPartials")

如何将id元素设置为href标记?我已经尝试过:

@Html.ActionLink("Questions","SectionPartials",new { id= "new-link" })

这会为a href添加一个id,但导航将不再起作用.这也会在代码中出错(但是会编译)

服务器错误:

enter image description here

代码错误

enter image description here

没有找到关于这个问题的更多信息..如果缺少某些信息(如控制器/视图(?)),我很乐意添加这些信息!

谢谢

解决方法

所需的LinkExtensions.ActionLink方法签名是:

public static MvcHtmlString ActionLink( this HtmlHelper htmlHelper,
string linkText,string actionName,object routeValues,object
htmlAttributes )

因此,您当前的新{id =“new-link”}将作为routeValues参数传递.您需要将其作为htmlAttributes传递:

@Html.ActionLink("Questions",null,new { id= "new-link" });

或(使用命名参数):

@Html.ActionLink("Questions",htmlAttributes: new { id= "new-link" });

见MSDN

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读