2

I need to mix and match JavaScript and C# using Razor syntax in the same statement. I could be using @: wrong for writing JavaScript inside of Razor code because the last expression in the Razor code block breaks all the JavaScript after the block.

var myVar = document.getElementById("myId");
var linkText = "someText";
var JSVar = X;  //X represents some number
@{
    int ID = JSVar;
    @:myVar.innerHTML = @Html.ActionLink(@:linkText, "Details", "Cards", new { Id = ID});
}

I've also tried to accomplish this as a single expression without the code block, like this:

myVar.innerHTML = @Html.ActionLink(@:linkText, "Details", "Cards", new { Id = @:IdVar });

This breaks the following JavaScript as well.

4
  • Where do you get the value of X? Commented Mar 27, 2017 at 18:17
  • X is irrelevant to the question. It represents a JavaScript var value (it's a number). Commented Mar 27, 2017 at 18:29
  • X doesn't even exist when while view is creating at server side. Are you trying to append X value to ULR when myId is clicked at client-side? Commented Mar 27, 2017 at 18:33
  • @Win Disregard 'X'. The variable is valid. The above is only an excerpt of my code, the only code in question is the Razor block. The JS variables are just to give readers context of how I'm trying to mix the JS and Razor. Commented Mar 27, 2017 at 18:47

2 Answers 2

1

Based on the question, it seems that you want to contract a link dynamically at client-side with value from JSVar.

If so, you could just append JSVar to URL. For example,

<input id="txtValue" type="text" value="Stackoverflow" />
<a id="myId">Navigate to Page</a>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#myId").click(function () {
            var JSVar = $("#txtValue").val();
            location.href = "@Url.Action("About", "Home")/id=" + JSVar;
        });
    })
</script>
Sign up to request clarification or add additional context in comments.

6 Comments

I didn't realize that you could mock an html element like that. However I am a little confused as to where 'location.href' comes from and what it represents.
Also I tried this since I posted the question: row.insertCell(0).innerHTML = '<a asp-action="Details" asp-controller="Cards" new {Id =' + cardId + '}>' + cardName + '</a>'; and it generates this : <a asp-action="Details" asp-controller="Cards" new {Id = "1}">cardName</a> and therefore renders the link useless due to incorrect syntax. I can't imagine why those quotations are appearing there.
You cannot place asp-* inside JavaScript. You need to approach from different angle such as using link's click event. You need to understand basic knowledge of jQuery and how DOM works; without jQuery you will be straggling with very basic thing in ASP.Net Core.
I'm fairly familiar with DOM and jQuery but I wasn't aware that jQuery was advantageous to asp.net core. Before this poject I used jQuery exclusively but I wanted to use basic library and master JS.
You can try row.insertCell(0).innerHTML = '<a href="@Url.Action("Details", "Cards", new {Id = cardId })">' + cardName + '</a>';. Obviously, cardId must be server-side variable.
|
0

I don't think this is possible to do. What I do (when I can't come up with a better alternative) is to store razor variables (like an href or model value) in a hidden field or div which I can then pull out via javascript.

What are you trying to accomplish exactly? There may be a more elegant way to achieve it?

1 Comment

I'm trying to make the innerHTML of a table cell a link to somewhere else in my app. I've inserted the 'linkText' in the html table already as plain text but am having trouble making it a link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.