so I’ve been struggling with this piece of code for hours now, I have the feeling its just a matter of wrong quotes or not escaping a symbol, but here is the problem.
I have a JSON call (in javascript) which loads a list of urls (article titles).
I then dynamically add those urls to a Div, and build my page using the document.createElement.
Problem is that I cant get the paramater, which is from the JSON list, to work in the Razor URL string.
var articleTitle = document.createElement("div");
articleTitle.className = "articleTitle";
articleTitle.innerHTML = '<a href="@Url.Action("Article", "Content", new { url = ' + article.Url + ', page = 1 }, null)">' + article.Title + '</a>';
articlewrapper.appendChild(articleTitle);
The { url = ‘ + article.Url + ‘,
is the problem here, I cant seem to get the actual value, the link now just prints out article.Url. The article.Title works just fine 🙂
Are article.Url and article.Title from javascript code retrieving JSON then? I would think your problem here would be that you’re mixing the .NET code with code from javascript.
The .NET would render first on the server, and then the javascript would take over on the client side after .NET had already done it’s job.
You may have to try something like: