Results 1 to 2 of 2

Thread: [RESOLVED] jsTree - can I load data from the viewbag?

  1. #1

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,710

    Resolved [RESOLVED] jsTree - can I load data from the viewbag?

    Hi All

    I'm just trying out the jsTree for the first time and like what I'm seeing. I've got it loading and formatting as I want and have it loading it's data using an ajax query to a method in my controller:-

    Code:
        <script type="text/javascript">
            $(function () {
                $("#ProductInterests").jstree
                    ({
                        "core" : { "themes" : { "icons" : false }},
                        "plugins": ["checkbox"],
                        "core" : 
                            {
                                "data": { 'url': '/Lead/GetProductInterestsJson', 'dataType': 'json' }
                            }
    
                    });
    
            });
        </script>
    The thing is, this is on a view that's being served up from an MVC controller. The controller populates a bunch of Select Lists into the ViewBag which the View then uses to populate various drop downs. It feels natural to populate the tree view in a similar way, i.e. by putting the necessary data into the ViewBag and then using that to populate the data in the View. Somehow having the View call to a method in the controller class just feels a bit odd in this circumstance.

    I've had a shufty at the jsTree documentation and a bit of a play with the syntax to see if I can get it to load from the ViewBag but I don't think it's supported. I thought I'd post here, though, to see if anyone knew different.

    edit> I should probably specify that this is jsTree3, found here. (I wasted a lot of time trying to get code snippets from previous versions to work before I figured out I was being a numpty)
    Last edited by FunkyDexter; May 21st, 2015 at 02:08 PM.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  2. #2

    Thread Starter
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,710

    Re: jsTree - can I load data from the viewbag?

    Found a way to do it. Quite obvious really. Just add the list to the View Bag and then use razor to loop across the list adding appropriate html elements:-
    Code:
            <div id="ProductInterests">  
                <ul>
                    @foreach (var productGroup in ViewBag.ProductGroups)
                    {
                        <li>
                            @productGroup.Name
                        </li>
                    }
                </ul>
            </div>
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width