J86
J86

Reputation: 15237

JavaScript Variable in C# Statement in Razor MVC

How can I access a JavaScript variable in C# code snippet within a Razor view? Here is an example of what I mean:

function mixIt(id) {
    var markup = '<div>' + @Model.Shaker.FirstOrDefault(x => x.ShakerID == id) + '</div>';
    return markup;
}

Notice where I am checking the id, that id is a javascript parameter. The code above isn't working, and I don't know how to get it to work. Google searching didn't bring anything useful either.

Upvotes: 2

Views: 4857

Answers (1)

Aaron Powell
Aaron Powell

Reputation: 25099

Simply put you won't be able to, the JavaScript code will be executed in the browser where as the C#/Razor will be executed on the server. This means that the result of your Razor snippet will be generated before the response is sent down to the browser.

Upvotes: 3

Related Questions