Giter Site home page Giter Site logo

dotnet-js-args's Introduction

Example of how to pass C# Variables to JS Files

  1. From within your view, you create a custom type inside a @function block

    @functions {
        public record HomeArgs(string name, int age) : IJavaScriptArgs { }
    }
  2. You can place your JS code in a scripts section if you prefer to control where it lands in the DOM:

    @section Scripts {
    
    }
  3. You then call @JavaScriptHelper.Args and pass in your path and object you'd like serialized:

    @JavaScriptHelper.Args("home/index", new HomeArgs("Kyle", 35))
  4. This will render the following raw html (script block) on your page:

    <script type="application/json" data-role="module-args" data-module-name="home/index">
      {"name":"Kyle","age":35}
    </script>
  5. Then call the script for the page however you like:

    <script type="text/javascript" src="@Url.Content("/js/home.js")"></script>
  6. Inside of the script, we can retrieve args by finding the inserted JSON and parsing it's contents. A helper function can grab the args based on path

    function getArgs(tsFilePath) {
        const selector = `script[type="application/json"][data-role="module-args"][data-module-name="${tsFilePath}"]`;
        const content = document.querySelector(selector).text
        return JSON.parse(content)
    }
  7. And then we can invoke that function to receive the serialized args from within our JS script

    args = getArgs("home/index") // {"name":"Kyle","age":35}

Run the project locally with dotnet run to try it out!

dotnet-js-args's People

Contributors

kylemit avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.