开发者

ASP .NET MVC 3 Design Problem (Loading data via Ajax)

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


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!


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

Thanks anyway...

Paul

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜