0

I have a class Category, that represents products category... So I created a View that shows a Treeview with my Categories on left ... When user select a TreeView Node, I´d like to show all the Category data in the same Page/View (On right)

What I did so far is load the JQuery TreeView via Ajax... And added a click event on each TreeView Node... The click calls, via Ajax, the selected Category, like that (CategoryController):

    [HttpGet]
    public ActionResult GetCategory(string idCategory)
    {
        if (ModelState.IsValid)
        {
            var _category = _categoryRepository.Get(Convert.ToInt16(idCategory));

            if (Request.IsAjaxRequest())                
                return Json(_category, JsonRequestBehavior.AllowGet);

            return RedirectToAction("Index");
        }

        return View();
    }

So, I got the Category JSON on client, and have to manually fill all the fields ...

Is that the right/best way to do that? Am I missing something?

Thanks so far

Paul

2
  • 1
    Are you using Bassistance JQuery TreeView plugin? Commented Mar 10, 2011 at 20:40
  • Never used Jstree, but I suppose it would require from you to create an action which returns JSON data containing category items, instead of your default view (unless it actually represents your JSON data). Commented Mar 10, 2011 at 20:58

2 Answers 2

1

If you're using JSTree, you can use the JSON_DATA plugin to return JSON data from your server and have JSTree build your nodes for you. I don't know what your _category object looks like, but the documentation provides the structure that JSTree expects.

{ 
    data : "node_title", 
    // omit `attr` if not needed; the `attr` object gets passed to the jQuery `attr` function
    attr : { id : "node_identificator", some-other-attribute : "attribute_value" }, 
    // `state` and `children` are only used for NON-leaf nodes
    state: "closed", // or "open", defaults to "closed"
    children: [ /* an array of child nodes objects */ ]
}

If you can't return data in this format, then you'll have to render the tree nodes manually as you're doing it now.

I hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but My jstree is ok already! Its working perfectly... My question is about the other part... Whats the best way to fill the category attributes when i click in some Node, in the same view that TreeView was built!
David, I am currently stuck in pulling the nodes from controller, can you provide an example that shows how you fill the data, state, and children in the controller? I will be really appreciative
David, or Paul can you give a simple example of how to return the json data from server side code ?
0

I resolve that using KnockoutJs with mapping plugin... It work great!

Thanks anyway...

Paul

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.