This is a migrated thread and some comments may be shown as answers.

Get model of the selected item

1 Answer 129 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
bashis
Top achievements
Rank 1
bashis asked on 14 Feb 2017, 08:05 PM

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:

@(
  Html.Kendo().TreeView()
      .Name("treeview")
      .DataTextField("Text")
      .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;
        }
         
        [HttpGet]
        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.

1 Answer, 1 is accepted

Sort by
0
Joana
Telerik team
answered on 16 Feb 2017, 05:17 PM

Hello,

 

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.

 

Regards,
Joana
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.
Tags
TreeView
Asked by
bashis
Top achievements
Rank 1
Answers by
Joana
Telerik team
Share this question
or