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

[Solved] TreeView checkedNodes items are null in postback

9 Answers 110 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kyle
Top achievements
Rank 1
Kyle asked on 14 Dec 2011, 07:45 PM
I am using the showCheckbox version of the MVC TreeView control, and the checked items nodes are not being populated on my postback. In this example, node.Text is always null, as well as everything else in node. Help would be much appreciated. 

Here is my View code:

@{
    List<TreeViewItem> checkedNodes = ViewData["TreeView1_checkedNodes"] as List<TreeViewItem>;
    @(Html.Telerik().TreeView()
        .Name("TreeView1")
        .ShowLines(false)
        .ShowCheckBox(true)
        .BindTo(Model.CurrentClinic.ClinicClinicians, mappings =>
        {
            mappings.For<AssureRx.Data.Entity.ClinicClinician>(binding => binding
                .ItemDataBound((item, clinicClinician) =>
                {
                    item.Text = clinicClinician.Clinician.Person.Title + " " +
                                clinicClinician.Clinician.Person.FirstName + " " +
                                clinicClinician.Clinician.Person.LastName + " " +
                                clinicClinician.Clinician.Person.Suffix;
                    item.Value = clinicClinician.ClinicClinicianId.ToString();
                     
 
                    item.Expanded = true;
                }));
        })
    )
}

And here is my postback controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult _PersonDetailPartial(List<TreeViewItem> checkedNodes, FormCollection collection, ClinicPerson model, string submitButton)
{
        string message = string.Empty;
        if (checkedNodes != null)
        {
            foreach (TreeViewItem node in checkedNodes)
            {
                message += node.Text + "<br/>";
            }
        }
        ViewData["message"] = message;
        ViewData["TreeView1_checkedNodes"] = checkedNodes;
 
        // Some other unrelated saving logic
 
    return PartialView("_PeopleDetailPartial", model);
}


9 Answers, 1 is accepted

Sort by
0
Shanmuga
Top achievements
Rank 1
answered on 19 Dec 2011, 11:06 AM
Hi Guys

I am having the same issue. It is exactly matching the same code they got in the demo application but still got the same issue. Is there anyone who can answer the fix for this?

Thanks
Shan
0
Alex Gyoshev
Telerik team
answered on 19 Dec 2011, 11:15 AM
You need to name the parameter like this: TreeView1_checkedNodes (where TreeView1 is the treeview name). The prefix is necessary in order to enable scenarios where multiple treeviews with checkboxes are on the page.

Greetings,
Alex Gyoshev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Shanmuga
Top achievements
Rank 1
answered on 19 Dec 2011, 11:37 AM
Hi
Thanks for your reply. I just figured it out accidently while going through your demo code. Unfortunately it is not clearly documented or I missed it some how.

There is an other issue now. Now I got something  inTreeView1_checkedNodes . It shows total items 3 or 4 depending on what I select in the checkbox list. But, while looping through, it shows, node.Value or node.Text as null. I am still confused why is that. Is there any reason why it can be like that?  Please note, I am binding the view to a complex model. Not directly to Employees. But to a viewmodel which got Employees as one of the property. Is that an issue again?

Thanks
Shan
0
Alex Gyoshev
Telerik team
answered on 19 Dec 2011, 01:06 PM

I assume that you get the null values after you have checked the checkboxes through JavaScript. In order to post the checkboxes correctly, you have to call the nodeCheck method.

Best wishes,
Alex Gyoshev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Kyle
Top achievements
Rank 1
answered on 19 Dec 2011, 08:49 PM
The problem ended up being that I was using the TreeView control on a partial view that was loading from an Ajax call. I didn't realize at the time there are special instructions in the online documentation regarding such situations.
0
Shanmuga
Top achievements
Rank 1
answered on 20 Dec 2011, 03:03 PM
Hi

There are some unexpected behaviour happening with this telerik treeview checkbox.  I got a view with list of checkboxes at the moment, only one level not even sub level. So there is no clientside scripting. There is only user checking the checkbox. No script involved.  I will be using this view for both create and update purposes.  For example in the database, I got User and menus.  One user can have more than one menus. I want to a admin page using treeview with checkboxes. I store that in a table called usermenus.  What is happening, when I load the page if the checkboxes are ticked while the page is loading, only those items got value in the chekced node collection back in controller after postback. If the checkboxes are checked on client side, then in postback, the value and Text are null. But the node appears in the collection.

It is not correct. Or am I missing something. Can you please update me asap,

Thanks
Shan
0
Alex Gyoshev
Telerik team
answered on 21 Dec 2011, 10:08 AM
Hello Shanmuga,

Have you included the TreeView scripts? They must be loaded in order to enable the proper checkbox handling. (either register them manually or through the ScriptRegistrar)

All the best,
Alex Gyoshev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Martin
Top achievements
Rank 1
answered on 24 Jan 2012, 10:42 AM
Hi Alex,
I inherited this problem from Shanmuga, I thought I would post the solution in case it saves someone else hours of frustration.  The problem was caused by a partial view rendered in the same view as the treeview (using Html.Partial).  In this partial view there was a self closing <p /> tag which caused the form to post back null values for the new checked nodes.  Changing this to XHTML compliant <p></p> fixed this issue for me.
Thanks,
Martin Ingham
0
Fajleabbas
Top achievements
Rank 1
answered on 09 May 2012, 06:40 AM
Hi Alex

I am having the same issue. It is exactly matching the same code they got in the demo application but still got the same issue.

as per your suggestion where i call the nodeCheck method of treeview.

Thanks
Abbas
Tags
TreeView
Asked by
Kyle
Top achievements
Rank 1
Answers by
Shanmuga
Top achievements
Rank 1
Alex Gyoshev
Telerik team
Kyle
Top achievements
Rank 1
Martin
Top achievements
Rank 1
Fajleabbas
Top achievements
Rank 1
Share this question
or