locked
The argument not passing to the javascript RRS feed

  • Question

  • User-1355965324 posted

    I have given the following code to pass Categoryname but not categoryname is  not coming to the javascript function, Please help

    @Html.ActionLink(item.CategoryName, "", null, new { onclick = "return TestFunc('" + item.CategoryName+ "');" })
    
    <script>
    
    function TestFunc(category) {
            alert(category);
            
        }

    Monday, February 10, 2020 5:05 PM

Answers

  • User711641945 posted

    Hi polachan,

    Because ActionLink has several constructor,what you did match the following code marked in green:

    public static IHtmlContent ActionLink(this IHtmlHelper helper, string linkText, string actionName, object routeValues, object htmlAttributes);        
           
    public static IHtmlContent ActionLink(this IHtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues);
            
    public static IHtmlContent ActionLink(this IHtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes);

    To fix such issue,you could change like below:

    @Html.ActionLink(item.CategoryName, "", null,null, new { onclick = "return TestFunc('" + item.CategoryName+ "');" })

    Or specify the parameter name:

    @Html.ActionLink(item.CategoryName, "", null, htmlAttributes:new { onclick = "return TestFunc('" + item.CategoryName+ "');" })
    

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 11, 2020 4:32 AM

All replies

  • User-474980206 posted

    view source to see what was rendered for the anchor.

    Monday, February 10, 2020 5:57 PM
  • User711641945 posted

    Hi polachan,

    Because ActionLink has several constructor,what you did match the following code marked in green:

    public static IHtmlContent ActionLink(this IHtmlHelper helper, string linkText, string actionName, object routeValues, object htmlAttributes);        
           
    public static IHtmlContent ActionLink(this IHtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues);
            
    public static IHtmlContent ActionLink(this IHtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes);

    To fix such issue,you could change like below:

    @Html.ActionLink(item.CategoryName, "", null,null, new { onclick = "return TestFunc('" + item.CategoryName+ "');" })

    Or specify the parameter name:

    @Html.ActionLink(item.CategoryName, "", null, htmlAttributes:new { onclick = "return TestFunc('" + item.CategoryName+ "');" })
    

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 11, 2020 4:32 AM