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

Ali Aziz 1 Reputation point
2022-04-21T12:41:04.943+00:00

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.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,157 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,252 questions
{count} votes