Seasonal Greetings,
How to get Checked Node values in Controller on form submit. I'm Using Tree Control (CheckboxMode.TriState) in MVC. Could any one please direct me with this. Appreciate your help
Hello Tk
To get the state of a checkbox in the igTree I recommend you use the checkState method:
https://www.igniteui.com/help/api/2014.2/ui.igtree#methods:checkState
If you need to get a specific node you can use nodeByIndex or nodeByPath:
https://www.igniteui.com/help/api/2014.2/ui.igtree#methods:nodeByIndex
https://www.igniteui.com/help/api/2014.2/ui.igtree#methods:nodeByPath
Please let me know if you have any further questions concerning this matter.
Hi Mike,
Thanks for reply. I'm looking for how to do it in MVC. The given samples are for binding data to View. It would be great if we have sample on how to get Checked node values in Controller. Please direct me on how to achieve this.
best regards,
tk
TK,
One approach you could take is to create a form on your MVC view and place a hidden field it. Upon submitting the form you would want to execute JavaScript logic to pull the checked nodes from the tree, and then serialize them in to a JSON string which you will store in the hidden field. The form will then send the string back to Controller where you can access it through Request.Form["<hidden field name>"]. This will be a string so you'll want to parse it out to actual data items but should work.
@using (Html.BeginForm()) { @(Html.Infragistics().Tree() .ID("tree") .DataSource(Model.Nodes.AsQueryable()) .Bindings(b => b.TextKey("Text") .ValueKey("ID") .ChildDataProperty("Children") .Bindings(child => child.TextKey("Text") .ValueKey("ID") ) ) .CheckboxMode(CheckboxMode.TriState) .DataBind() .Render() ) @Html.Hidden("CheckedNodes"); <input id="submit" type="submit" value="Submit"/> } <script> $(function () { $('#submit').click(function () { var checkedNodes = $('#tree').igTree('checkedNodes'); var checkedItems = []; for (var i = 0; i < checkedNodes.length; i++) { checkedItems.push(checkedNodes[i].data); } var itemString = JSON.stringify(checkedItems); $('#CheckedNodes').val(itemString); }) }) </script>
@using (Html.BeginForm()) { @(Html.Infragistics().Tree() .ID("tree") .DataSource(Model.Nodes.AsQueryable()) .Bindings(b => b.TextKey("Text") .ValueKey("ID") .ChildDataProperty("Children") .Bindings(child => child.TextKey("Text") .ValueKey("ID") ) ) .CheckboxMode(CheckboxMode.TriState) .DataBind() .Render() ) @Html.Hidden("CheckedNodes"); <input id="submit" type="submit" value="Submit"/> }
<script> $(function () { $('#submit').click(function () { var checkedNodes = $('#tree').igTree('checkedNodes'); var checkedItems = [];
for (var i = 0; i < checkedNodes.length; i++) { checkedItems.push(checkedNodes[i].data); } var itemString = JSON.stringify(checkedItems);
$('#CheckedNodes').val(itemString); }) }) </script>