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

前端 未结 6 1589
-上瘾入骨i
-上瘾入骨i 2020-12-05 04:24

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

i wan to call one method which is in java script but how to call it within html.actionlink in same

6条回答
  •  时光说笑
    2020-12-05 05:04

    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

    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. You can read more about that at:

    What's the effect of adding 'return false' to a click event listener?

    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. 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.

提交回复
热议问题