Knockout JS and Razor - How to assign data to Razor variables?

Ali Aziz 1 Reputation point

I am working with a multi layer web application that uses various web technologies. It interacts with a WebAPI to get the data via Knockout JS.

I will try to narrow the scope of my question to this:

I have a span tag

<span id="title" data-bind="text: FullTitle"></span>  

I would like to get the content of that span and assign it to a Razor variable.

I have tried:

 var Test = <span id="title" data-bind="text: FullTitle"></span>  

But this doesn't render the knockout code and it just assigns the full span code to that variable. I just want to get the full title when knockout pulls it into the Test variable.

Note: the span works outside the razor block and displays the full title on the cshtml view.

Any ideas?

Thanks in advance.

A set of technologies in the .NET Framework for building web applications and XML web services.
3,150 questions
A Microsoft web application framework that implements the model-view-controller (MVC) design pattern.
852 questions
{count} votes