最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ASP.NET MVC RAZOR ActionLink click - Stack Overflow

programmeradmin1浏览0评论

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.

@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:

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

For some reason this is not working as expected.

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.

@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:

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

For some reason this is not working as expected.

Share Improve this question edited Jul 15, 2013 at 13:37 anar khalilov 17.5k9 gold badges51 silver badges63 bronze badges asked Jul 15, 2013 at 13:25 SrinivasSrinivas 2,53910 gold badges48 silver badges70 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

You were not binding event at when DOM is pletely loaded.

<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

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. You can do this as well:

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

评论列表(0)

  1. 暂无评论