Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Thomas 24 posts 104 karma points
    Sep 24, 2018 @ 09:33
    Thomas
    0

    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?

  • Jan Skovgaard 11264 posts 23592 karma points MVP 8x admin c-trib
    Sep 24, 2018 @ 10:42
    Jan Skovgaard
    0

    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

  • Thomas 24 posts 104 karma points
    Sep 25, 2018 @ 05:34
    Thomas
    0

    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.

    <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?

  • louisjrdev 107 posts 343 karma points c-trib
    Sep 25, 2018 @ 07:14
    louisjrdev
    0

    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,

Please Sign in or register to post replies

Write your reply to:

Draft