Copied to clipboard

Flag this post as spam?

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


  • Rachel Wong 1 post 71 karma points
    Feb 09, 2020 @ 01:56
    Rachel Wong
    0

    Passing values from view .cshtml to javascript

    Hi everyone. I'm new to umbraco, and learning by doing and reading documentation.

    I currently don't have any custom controllers yet. I am grabbing & parsing the data from CMS to the Views by setting up something like the below at the top of every .cshtml file

    @{ var allDataToDisplay = Model.Root().Descendants(ContentType.SomeDocType).ToList(); } I realise this may not be ideal.

    I want to pass data from the CMS (i.e. allDataToDisplay variable) to some javascript at the bottom of .cshtml to render/reload the page in some way. The original intent is to store query string via javascript to create a multi-dropdown list sort.

    ``` @foreach (var item in allDataToDisplay) {

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Feb 10, 2020 @ 12:31
    Michaël Vanbrabandt
    0

    Hi Rachel,

    first of all welcome to the Umbraco Community!

    Can you give some more details about what you are trying to do because its not completely clear what you are trying to achieve.

    Do you want to render a dropdown based on this list and use javascript to perform an url change with a querystring? Is that correct or... ?

    /Michaël

  • Ryan Helmn 26 posts 188 karma points
    Feb 10, 2020 @ 13:54
    Ryan Helmn
    0

    Example of how I've used Razor within JavaScript:

            var priceSlider = function() {
                $("#price-slider").slider({
                    range: true,
                    min: 1,
                    max: @Model.Products.Select(x => x.Price.DefaultCurrencyValue).Max(),
                    values: [
                        @Model.FilterModel.FromAmount,
                        @(Model.FilterModel.FromAmount > 0 ? Model.FilterModel.FromAmount : Model.Products.Select(x => x.Price.DefaultCurrencyValue).Max())
                    ],
                    slide: function(event, ui) {
                        $("#fromAmount").val(ui.values[0]);
                        $("#toAmount").val(ui.values[1]);
                    }
                });
    

    Just by adding the '@' before your c# variable within the js calls that variable.

Please Sign in or register to post replies

Write your reply to:

Draft