简体   繁体   中英

Calling Html Helper Method from Javascript Function

I am trying to call a custom Html helper method that I have written from a javascript function that is used by jqGrid to return formatted text, in this case a link, for a cell:

function formatGroupPlanEditLink(cellValue, options, rowObject) {
        //var cellHtml = "<a href='/Insurance/GroupPlanEdit/?id=" + rowObject[0] + "'>" + rowObject[1] + "</a>";
    var functionArgs = rowObject[1] + ',Url.Action("GroupPlan", "Insurance", new { id = ' + rowObject[0] + ' }),String.Format("Edit {0}", ' + rowObject[1] + '), listId,Url.Action("GroupPlanList", "Insurance"),false';
    var cellHtml = '@Html.DialogFormLink(' + functionArgs + ')';
    return cellHtml;
}

The problem that I have is that I cannot concatenate the entire string before the helper is executed. So the browser is trying to execute "@Html.DialogFormLink(" - which of course causes an error. I guess there must be a better way to go about this. I really want to still be able to use the Html helper method as I use it elsewhere, and it works nicely for my requirements.

I'm not that familiar with Razor, but the quotes around the @Html helper look suspicious.

var cellHtml = '@Html.DialogFormLink(' + functionArgs + ')';

The browser doesn't execute @Html.DialogFormLink; the server evaluates it. I suspect the server is injecting the literal '@Html.DialogFormLink' into your js.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM