Get model of the selected item

2 posts, 0 answers
  1. bashis
    bashis avatar
    1 posts
    Member since:
    Feb 2017

    Posted 14 Feb Link to this post

    Good day. This must be a very basic thing to do, though I didn't manage to find any information on it.


    I have a treeview setup like this:

          .DataSource(dataSource => dataSource.Read(read => read.Action("GetView", "Tree"))

    My controller looks like this:

        public class TreeController : Controller
            private readonly IRepository _repository;
            public TreeController(IRepository repository)
                _repository = repository;
            public JsonResult GetView()
                var items = _repository.GetItems();
                var viewmodel = items.Select(t => new
                    id = t.Id,
                    Text = t.Text,
                    hasChildren = t.HasChildren
                return Json(viewmodel, JsonRequestBehavior.AllowGet);

    That part works just fine.

    Now, I would like to add a button that would, say, rename the selected item. So,I know how to get the selected item in the treeview. What I do not know how to implement is the connection viewmodel -> data model. That is, knowing the selected html item I have no idea how to get the model item it coresponds to. That would be solvable if I could add my custom html "model-id" property on every item and pass it back to controller on my button click. Which I have no idea how to implement either.


    Thank you in advance.

  2. Joana
    Joana avatar
    135 posts

    Posted 16 Feb Link to this post



    Note that updating the model and persisting a component's settings concerns MVC and not the Treeview widget. The developer needs to implement the logic for his project. However, as an exception, I have  attached a sample MVC project that shows a rename button for each Treeview node and updates the model with a new value provided by the user. Generally, I've covered almost the same scenario, but you'll need to style it and adjust the logic to your project.


    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top