Actually here i'm using inline java script to read umbraco model value and assigned it to global variable (for doing some validation in external java script) like below
in cshtml.
<script type="text/javascript">
var Name = @(Model.Content.GetPropertyValue("Name"));
</script>
instead of using this inline i want to move that code to external java script file means, i want to read the umbraco model value in external java script file. how this is possible? is the umbraco has any service to read model property value from java script?
As Jan said this will have to be done by creating a rest service, this is because when you are in you're .cshtml file, you have the context of the Model, as soon as you leave your view, you have no access to that server side data (as javascript is a client side file) so you will not be able to reference the Model in your JS file.
You could as Jan said create an API endpoint to call from your external JS file that returns the data you need,
Read umbraco model value in external java script file
Hi, I want to read umbraco model value in external javascript file not via internal like using javascript tag in cshtml page.
How this is possible and is there any feature in umbraco?
Hi Thomas
If I understand you correctly you'll need to create a REST service, which you can achieve by using Web API - You can read and learn more about it in the documentation here https://our.umbraco.com/Documentation/Reference/Routing/WebApi/
I hope this helps!
/Jan
Hi Jan
Thanks for the update
Actually here i'm using inline java script to read umbraco model value and assigned it to global variable (for doing some validation in external java script) like below
in cshtml.
instead of using this inline i want to move that code to external java script file means, i want to read the umbraco model value in external java script file. how this is possible? is the umbraco has any service to read model property value from java script?
Hi Thomas,
As Jan said this will have to be done by creating a rest service, this is because when you are in you're .cshtml file, you have the context of the Model, as soon as you leave your view, you have no access to that server side data (as javascript is a client side file) so you will not be able to reference the Model in your JS file.
You could as Jan said create an API endpoint to call from your external JS file that returns the data you need,
is working on a reply...