Passing a JavaScript parameter into a RAZOR function from the CodeBehind

I am facing an issue with my code. I need to pass the value of a JavaScript variable X as a parameter in the Razor function EXTENDED.ProcessDay()

 function Test() {
                var X = document.getElementById("targetID").value;
                document.getElementById("result").value = "@EXTENDED.ProcessDay(X)";
            }

Unfortunately, the above code is not working as expected. I have also tried:

function Test() {
                var X = document.getElementById("targetID").value;
                document.getElementById("result").value = "@EXTENDED.ProcessDay(@X)";
            }

However, this approach did not yield the desired result either. Can someone guide me on the correct method to achieve this?

Answer №1

Attempting to execute Javascript and C# code simultaneously is not possible. Razor variables are specific to the server side and cease to exist once the page has been transmitted to the client side.

Answer №2

In my interpretation, Makif was pointing out the distinction between how Javascript dynamically alters elements on a live webpage versus Razor's pre-rendering capabilities that act before page loading. For example, in Javascript, clicking a button can immediately change an input value, while in Razor, defining x as 2 will set the input value to 2 once the page is loaded. It's essential to note that you cannot manipulate a Razor function using Javascript; you must either rely on a comprehensive JS function or a complete Razor function for all operations.

While I consider myself well-versed in Razor development, my knowledge of JS is still expanding. This insight may provoke discussion among seasoned JS developers.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Type parameter for unprocessed JSON information

I am currently facing an issue with a script that communicates with my API and returns the response to the caller. I am having trouble defining the type correctly for this operation. The responses from the API always consist of a boolean flag, success, in ...

The datatable fails to render after executing a function in AngularJS

When I load the page without calling a function, the data is displayed in a datatable perfectly fine. However, if I try to generate the datatable after calling a function, it does not work. HTML: <div class="widget-body no-padding"> ...

Why is it necessary to call the .call(this) method when extending a THREE.js "class"?

Currently, I am in the process of creating a new object that should inherit from THREE.Object3D(). In my code snippet, you can see how I have set it up: function myNewObject() { THREE.Object3D.call(this); //... } myNewObject.prototype = new THREE.O ...

Can onerror be triggered on an HTML Image onload event?

Having an html image labeled as follows: <image src = 'image.png' onerror = "handleError(this)" onload = "handleLoad(this)"> function handleError(n){ getFirstChild(n.parentNode).style.display = '',n.style.display ...

Issues with the functionality of the WordPress plugin

The issue with Spin360: All scripts are linked in functions.php add_action('wp_footer', 'add_scripts'); function add_scripts() { if(is_admin()) return false; wp_deregister_script('jquery'); wp_enqueue_script ...

What causes jQuery to output individual characters instead of the entire content of the span element when looping through it?

I am facing an issue with a group of span elements that have the "endtime" class. My goal is to retrieve each of their contents separately from one another. <span class='endtime'> 2011-03-29 00:01:03 </span> <span class='e ...

Setting the default date in angular-bootstrap-datetimepicker: a step-by-step guide

I am currently utilizing the angular-bootstrap-datetimepicker module for allowing users to choose a date and time. How can I set a default date and time when the view initially renders? Is there an attribute available that allows us to assign a boolean val ...

In development, asp.net code runs with lightning speed, but in production, it seems to move

On my development PC, the code for constructing a large string runs smoothly, taking only 2 seconds to finish all tasks. However, when I move it to the production server, the process slows down significantly, taking anywhere from 10-20 seconds due to an un ...

Improving $rootScope.$apply functionality in Angular

With my MusicPlayer.js Angular service, I have a callback function that updates a specific object (musicPlayer.currentPlaybackTime) and is shared among all controllers by using $rootScope.$apply. However, I am aware of the potential issue of polluting the ...

"Exploring the process of manipulating local storage and the potential for

I am in the process of developing a web application that heavily relies on localstorage. Currently, I have it configured to save data to the local storage using this specific format: Key: Four digit number Value: My data Now, my goal is to gather all the ...

It is not possible to invoke the Ajax function on the subsequent page, while using AJAX and PHP

My current ajax function has a total of 7 parameters, but the focus today is on the fifth parameter known as "stop". When this parameter is set to "true", the ajax function halts the setTimeout feature and essentially stops calling itself unless triggered ...

Using Python 3.2 to Implement Basic HTTP Authentication with urllib.request

Hey everyone, I'm reaching out for some help here as I've hit a roadblock with my project. I've been struggling for the past week to get this functionality to work, so any assistance is greatly appreciated. The issue I'm facing involve ...

Changing the position of a Bootstrap popover dynamically from top to bottom

Struggling with the bootstrap popover here. I can't seem to change the popover position from top to bottom when it reaches the top of the viewport. I attempted to use placement: 'auto bottom' but unfortunately, it didn't do the trick f ...

Bugs with XPATH in Ranorex

Currently using Ranorex tools for testing, I've noticed that the application I'm working on is .NET based. To capture objects, we typically use the XPATH of a specific object and then utilize this XPATH to verify if the object is present or not. ...

tips on adjusting the page results and maximum results for a dataTable

I have retrieved over 100 rows from MySQL, which is the default limit for displaying results in DataTables. I want to change the default limit of Datatables to display 30 rows on each page until all my fetched MySQL results are shown. When querying M ...

What is the best way to upload multiple textures using the latest THREE.TextureLoader?

Can anyone advise on how to efficiently load multiple textures using the new THREE.TextureLoader from Three.js? Currently, I am loading my textures individually as shown below: var texture1 = THREE.ImageUtils.loadTexture('texture1.jpg'); va ...

Obtaining input data from a form using Node.js

I'm facing an issue with my node.js app where the form data is not getting sent properly via POST request. I have tested the post endpoint using Postman and it works fine, but when I submit the form, the database receives an empty object without any o ...

The random string generator is selecting a unique string for both the server and client side

I am facing an issue with a component in Next.js (v14.2.1) where I need to display a random string while fetching data. Despite my attempts, I can't seem to maintain the same string on both server and client sides. Below is the code snippet I am curr ...

Submitting form based on HTML select input issue

I am facing an issue with a select form where the value resets to "10" every time I send the form. Is there a way to keep the selected option, for example "20", after submitting the form? Below is the code snippet: <form method='get' name=&a ...

Updating previously submitted data using rendering in Node.js and Express.js

I'm having trouble updating data in real-time from my router.js to be used in JavaScript without refreshing the page. I need a way to pass the new coordinate data to the page when a certain condition is met, all without reloading. router.get('/&a ...