简体   繁体   English

如何在asp.net mvc 的html.actionlink 中调用javascript 函数?

[英]how to call javascript function in html.actionlink in asp.net mvc?

how to call javascript function in html.actionlink in asp.net mvc?如何在asp.net mvc 的html.actionlink 中调用javascript 函数?

i wan to call one method which is in java script but how to call it within html.actionlink in same page thanks in advance我想调用一个在 java 脚本中的方法,但如何在同一页面的 html.actionlink 中调用它,在此先感谢

you need to use the htmlAttributes anonymous object, like this:您需要使用 htmlAttributes 匿名对象,如下所示:

<%= Html.ActionLink("linky", "action", "controller", new { onclick = "someFunction();"}) %>

you could also give it an id an attach to it with jquery/whatever, like this:你也可以用 jquery/whatever 给它一个 id 附加到它,像这样:

<%= Html.ActionLink("linky", "action", "controller", new { id = "myLink" }) %>


$('#myLink').click(function() { /* bla */ });

For calling javascript in your action link you simply need to write actionlink like this:要在您的操作链接中调用 javascript,您只需像这样编写 actionlink:

@Html.ActionLink("Delete", "Your-Action", new { id = item.id },
                 new { onclick="return confirm('Are you sure?');"})

Don't get confused between route values and the html attributes.不要混淆路由值和 html 属性。

<a onclick="MyFunc()">blabla..</a>

There is nothing more in @Html.ActionLink that you could utilize in this case.在这种情况下,@Html.ActionLink 中没有什么可以使用的了。 And razor is evel by itself, drop it from where you can.剃刀本身就是 evel,从你可以的地方扔掉它。

@Html.ActionLink("Edit","ActionName",new{id=item.id},new{onclick="functionname();"})

This is a bit of an old post, but there is actually a way to do an onclick operator that calls a function instead of going anywhere in ASP.NET这是一篇旧帖子,但实际上有一种方法可以执行调用函数的 onclick 运算符,而不是在 ASP.NET 中的任何地方

helper.ActionLink("Choose", null, null, null, 
    new {@onclick = "Locations.Choose(" + location.Id + ")", @href="#"})

If you specify empty quotes or the like in the controller/action, it'll likely add a link to what you listed.如果您在控制器/操作中指定空引号等,它可能会添加指向您列出的内容的链接。 You can do that, and do a return false in the onclick.您可以这样做,并在 onclick 中返回 false。 You can read more about that at:您可以在以下位置阅读更多相关信息:

What's the effect of adding 'return false' to a click event listener? 将“return false”添加到单击事件侦听器有什么影响?

If you're doing this onclick in an cshtml file, it'd be a bit cleaner to just specify the link yourself (a href...) instead of having the ActionLink handle it.如果您在 cshtml 文件中执行此 onclick 操作,则自己指定链接(a href ...)而不是让 ActionLink 处理它会更简洁一些。 If you're doing an HtmlHelper, like my example above is coming from, then I'd argue that calling ActionLink is an okay solution, or potentially better, is to use tagbuilder instead.如果你正在做一个 HtmlHelper,就像我上面的例子一样,那么我认为调用 ActionLink 是一个不错的解决方案,或者可能更好,而是使用 tagbuilder。

This is the only one that worked for me in .cshtml file:这是 .cshtml 文件中唯一对我有用的:

@Html.ActionLink(
   "Name", 
   "Action", 
   "Controller", 
   routeValues: null, 
   htmlAttributes:new Dictionary<string, object> {{ "onclick", "alert('Test');" }})

I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在asp.net mvc中的html.actionlink中调用javascript确认对话框功能? - how to call javascript confirm dialog function in html.actionlink in asp.net mvc? 使用@ Html.ActionLink发布textarea-ASP.NET MVC 5 - Post textarea using @Html.ActionLink - ASP.NET MVC 5 c#asp.net mvc-使用参数从html.actionlink调用脚本函数 - c# asp.net mvc - calling script function from html.actionlink with a parameter 如何在ASP.NET MVC 4中通过Html.Actionlink触发window.open - How to trigger window.open by Html.Actionlink in ASP.NET MVC 4 在ASP.Net MVC中,如何在单击时显示和隐藏3个@ Html.ActionLink按钮? - Within ASP.Net MVC, how do I show and hide 3 @Html.ActionLink buttons on click? 如何在html.actionlink中仅调用javascript函数 - How to call ONLY javascript function in html.actionlink ASP.NET MVC ActionLink和javascript函数 - ASP.NET MVC ActionLink whith javascript function MVC相当于@ Html.ActionLink通过javascript - MVC equivalent of @Html.ActionLink through javascript 为什么在加载视图和单击ASP.NET MVC中的Html.ActionLink之间,我的routeValues变量会发生变化? - Why is my routeValues variable changing between my view loading and me clicking on my Html.ActionLink in ASP.NET MVC? 如何在JavaScript中模拟对@ Html.ActionLink的点击? - How to simulate a click on a @Html.ActionLink in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM