JQuery
JQuery

Reputation: 907

@Html.ActionLink adding class and id

trying to add a class to an action link and id value, capture the class and pass the value to the controller.

I have this Jquery to capture the link click:

<script>
$(function () {
    $(".ActionLink").click(function (e) {
        e.preventDefault();
        var newurl = $(this).attr("href");            
        console.log(newurl);
        $("#AddSection").load(newurl);            
    });
});

tried a few different ways without success.

First one is

@Html.ActionLink("Edit", "_EditGender1", new { id = item.GenderID })

which returns the html: <a href="/Treeview/_EditGender1/1">Edit</a>

obviously this one won't work but it assigned the id correctly to the end of the url /1

second one:

@Html.ActionLink("Edit", "_EditGender1", new { @class = "AddSection", id = item.GenderID })

which returns: <a href="/Treeview/_EditGender1/1?class=AddSection">Edit</a>

The id again is added correctly, but even though it has the class=AddSection the Jquery does not capture this.

 @Html.ActionLink("Edit", "_EditGender1", "Treeview", null, new { @class = "AddSection", id = item.GenderID })  

returns: <a class="AddSection" href="/Treeview/_EditGender1" id="1">Edit</a>

This is captured by the Jquery correctly, but it isn't passing the id into the url and back to the controller.

Not sure what else to try!

Upvotes: 0

Views: 1599

Answers (1)

user3559349
user3559349

Reputation:

@Html.ActionLink has an overload to add route values and html attributes.

@Html.ActionLink("Edit", "_EditGender1", new { id = item.GenderID }, new { @class = "AddSection" })

The 3rd parameter adds route values and the 4th parameter adds html attributes.

Refer documentation

Upvotes: 3

Related Questions