Arthur Swails Arthur Swails - 2 months ago 14
ASP.NET (C#) Question

ASP.NET Razor mixing with javascript literal

My problem is when I try to setup the onclick handler, updatePage(), is there a way to inject a Razor variables value as a parameter to the updatePage() function like I'm trying to do below? What I have below of course is a compile error caused by the updatePage portion.

@Html.ActionLink("Next >", "Index",
new { page = iPageT + 1 },
new { @class = "paging-links-submit", onclick = "updatePage("iPageT++"); return false;" })


You need to concat your server side processed razor argument with the client side javascript function call. Add the + like this:

@Html.ActionLink("Next >", "Index",  
    new { page = iPageT + 1 },   
    new { @class = "paging-links-submit", onclick = "updatePage(" + iPageT + "); return false;" })

I would prefer to increment iPageT before calling @Html.ActionLink(), but this is up to you.