I have a JS function which I want to loop round a c# object passed from the controller. I then want to compare it to a HTML input value - which I have passed into the JS function as a string.
Function is called like
searchReleases($("[name=release]").val());
function searchReleases(givenName) {
var list;
var data = givenName;
#foreach (var a in #Model.Releases) {
if (#a.Name.Contains(givenName)) {
list+= a.Name + "=";
}
}
}
However I can't access the JS variable givenName within the IF statement.
I have spent a while trying to find an answer on google but have yet found a workable solution.
You should understand that C# is a server-side code and JS is executed on client-side.
So if you generate view in ASP.MVC you have access only to C# data. All information which you want to print on your page will be statically generated to HTML file.
Then you can pass this HTML page to browser and inside it, it would be executed JS code. But only based on statically generated data or JS variables.
You can't read JS from C#, but you can read C# from JS. So if it's a read-only operation, than convert your C# object to JSON and access it from the JS code.
Try this answer : Asp.net mvc passing a C# object to Javascript
Related
Im using Hugo and Im trying to pass a markdown parameters from md file to JavaScript file.
This is the markdown file
This is my javascript file
Are you using your own theme, or something you have downloaded?
You can edit the relevant template to output the parameter as a Javascript variable. And after that, use a Javascript function to reference that variable by name. Example:
<script type="application/javascript">
var title = "{{.Title}}"
console.log(title)
</script>
In this case, you would probably want to add a check to ensure that parameter exists, or is properly set before trying to print it. Otherwise the JS may throw an error if the variable is null or undefined.
Hugo is a static website generator - so it outputs HTML - if you want a "variable" from markdown to Javascript - with traditional JS and Markdown (i.e. no products like Alpine or something beyond the scope of Hugo) you would output the HTML and have Javascript - getElementbyID for instance.
I am trying to execute Java method in Javascript over object which I get from Java code using Freemarker.
I have java object "actual_id" of type "Settinga" and method "setActual_id" which just sets one attribute of the object. This is my call of the method in js:
var idd = ${actual_id}.setActual_id(variable);
Instead of execution in html code I see:
javaclassurl$Settinga#5beccfce.setActual_id(variable);
And there is error: myFunction is not defined
at HTMLButtonElement.onclick
Why it doesnt execute?
Following call is executed as expected but I need to include variable:
var idd = "${actual_id.setActual_id("aaa01dd3-abe4-4d50-a69e-62b04199b7c5")}";
I understand that Freemarker expressions are translated during page generation and when I use just string its easily interpreted and can be executed but during page generation my variable which I want to include in the call isnt known so need to make execution somehow else...
Because the way it works is this: FreeMarker is executed on the server, resolving all the ${...} to mere text, then the browser on the client side gets the resulting HTML and executes the JavaScript.
I have serialize object returning from Controller to blade view in laravel like:
"a:3:{i:0;s:1:"2";i:1;s:1:"4";i:2;s:1:"6";}"
from my blade view I use this JS code block to get those value as array.
var branches = {{unserialize($preliminary->branches)}};
But in there I'm getting error saying
expression expected
any suggestions to solve this situation..?
Run json_encode on top of your unserialize.
E.g.
var branches = {{json_encode(unserialize($preliminary->branches))}};
unserialize is giving you a PHP object, which you are trying to inject directly into JS. By passing it through json_encodeyou convert it to a string javascript can grok.
I want to set the javascript variable value to flask template variable in javascript. What I am trying is
$(document).on("click", ".prepopulate", function () {
var myBookId = $(this).data('id');
alert(myBookId); // The value is showing proper
{% set tempVar = 'myBookId' %}
alert ({{tempVar}})
});
But it's giving an error instead (UndefinedError: 'list object' has no attribute 'myBookId'). What is the way to set the template variable in javascript using javascript variable?
You want to use set like this:
%SET{"tempVar" value="myBookId"}%
You cannot do this because jinja runs before the page loads and on the server side but the javascript code is executed when the page is loading and on the client side (browser) so your myBookId variable doesn't exist for jinja (see this answer for more info). A way to achieve what you want is to use ajax. See here an example.
If you look at your rendered HTML you will see
alert (myBookId)
Unless you define a variable called myBookId, this is not valid JavaScript. You need to wrap the string value in quotes.
alert('{{ myBookId }}')
An even better way to do this is to let Jinja decide for you if quotes are needed.
alert({{ myBookId|tojson|safe }})
This will wrap string values in strings, leave integers alone, and use JavaScript booleans.
How can I call a .net method in inline code using a javascript variable?
My code looks like this:
for (var i = 0; i < numberIterations; i++)
{
var result = <%# GetFilterTagURL( myArray[i].Value, false) %>;
//do stuff with result
}
This block is inside a javascript block; the GetFilterTagURL method is a a .net method and I want to pass the variable myArray[i].Value as the first parameter.
Any ideas?
Update:
All your answers are helpful.
I guess that, as you say, I'll have to create a web service to achieve what I want.
You will not be able to accomplish it like that, since the <%# %> tag is used for Data binding.
One approach however would be to create a webservice and call it from your javascript. Inside your webservice you can then make the call to GetFilterTagURLand use the result in your javascript.
Check out this article from MSDN for more info.
You cannot send client-script values to the server-side. The oposite is valid though, you can register a javascript block from your code-behind.
See Page.RegisterStartupScript method.
this will work, but you have to call Page.DataBind() in your code