ASP.NET MVC RAZOR ActionLink单击

Sri*_*vas 1 javascript asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我在视图中有此操作链接:

@Html.ActionLink("Edit", "Edit", null, new {@id="editLink", @class="button"})
Run Code Online (Sandbox Code Playgroud)

我想调用一个java脚本方法在运行时从我的Selected List Box Item中生成URI.

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

作为第一步,当我尝试使用JavaScript显示警报时:

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

由于某种原因,这没有按预期工作.

Sat*_*pal 5

在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>
Run Code Online (Sandbox Code Playgroud)

其次,make the URI at runtime from my Selected List Box Item你必须停止它所使用的默认行为return false