简体   繁体   中英

Passing a JavaScript variable to a helper method

I am using ASP.NET MVC 3 and the YUI library.

I created my own helper method to redirect to an edit view by passing in the item's ID from the Model as such:

window.location = '@Url.RouteUrl(Url.NewsEdit(@Model.NewsId))';

Now I am busy populating my YUI data table and would like to call my helper method like above, not sure if it is possible because I get the item's ID by JavaScript like:

var formatActionLinks = function (oCell, oRecord, oColumn, oData) {
   var newsId = oRecord.getData('NewsId');
   oCell.innerHTML = '<a href="/News/Edit/' + newsId + '">Edit</a>';
};

Store your URL with some custom ID, which you'll now to replace later on:

var myurl = '@Url.RouteUrl(Url.NewsEdit(0))'; //Let's use zero

later on:

var formatActionLinks = function (oCell, oRecord, oColumn, oData) {
   var newsId = oRecord.getData('NewsId');
   //lets use the url we defined above, and replace the zero with our id.
   oCell.innerHTML = '<a href="' + myurl.replace('0',newsId)+ '">Edit</a>';

};

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