简体   繁体   English

ASP.NET MVC RAZOR ActionLink请点击

[英]ASP.NET MVC RAZOR ActionLink click

I have this action link in view: 我有此操作链接:

@Html.ActionLink("Edit", "Edit", null, new {@id="editLink", @class="button"})

I would like to call a java script method to make the URI at runtime from my Selected List Box Item. 我想调用Java脚本方法在运行时从“选定列表框项目”中创建URI。

@Html.ListBox("EmployeeName", (IEnumerable<SelectListItem>)ViewBag.selectlist, new { @id = "saglistbox", @class = "SAGListbox" })

As a first step, when i am trying to just display an alert using JavaScript: 第一步,当我尝试仅使用JavaScript显示警报时:

<script type="text/javascript">
    $('#editLink').click(function () {
        alert("hello");
    });
</script>

For some reason this is not working as expected. 由于某种原因,这无法正常工作。

You were not binding event at when DOM is completely loaded. 当DOM完全加载时,您没有绑定事件。

<script>
$(function () {
    $('#editLink').click(function () {
        alert("hello");

        //To get selected value use
        var selected = $("#EmployeeName").find(':selected').text();

        //To stop default behaviour
        return false;
    });
});
</script>

Second make the URI at runtime from my Selected List Box Item then you have to stop it default behavior thus used return false 其次make the URI at runtime from my Selected List Box Item然后必须停止它的默认行为,因此使用return false

Try this: 尝试这个:

<script type="text/javascript">
    $(document).ready(function()
    {
        $('#editLink').click(function ()
        {
            alert("hello");
        });
    });
</script>

You can pass any html attribute as parameter to generate the action link. 您可以将任何html属性作为参数传递来生成操作链接。 You can do this as well: 您也可以这样做:

@Html.ActionLink("Edit", "Edit", null, new { @id="editLink", @class="button", onclick = "myfunction()" })

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM