Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

Can anybody tell me how to reload a view in MVC3 using javascript.

Here is my scenario :

In View i placed a JQTreeview and i am binding that tree with database from controller method using DataUrl, It is a property of JQTreeview. I have to add new node based on selected item of the tree. At this point i am trying to reload the page but control is not going to the controller method.
For reloading page i am using window.location.reload(); in javascript.
Code:
C#
MVC@Html.Trirand().JQTreeView(
                new JQTreeView                {
                    DataUrl = Url.Action("TreeView_TextAndImages_DataRequested"),
                    Height = Unit.Pixel(300),
                    Width = Unit.Pixel(300),
                    ClientSideEvents = new TreeViewClientSideEvents { Select = "SelectNode" },
                }, "treeview")Here is the controller method

public JsonResult TreeView_TextAndImages_DataRequested()        {
            List<jqtreenode> nodes = new List<jqtreenode>();
            ListAllItems();            
            if (lstItems.Count == 0)            {
               nodes.Add(new LeafNode { Text = "New.." });            }
            else  {
                nodes.Add(new LeafNode { Text = "New..", Expanded = true });
                if (check)
                    nodes.Add(new LeafNode { Text = "New Accumulator <date time="">..", Expanded = true });
                var node = nodes.Find(n => n.Text == "New..");
                foreach (TreeItems item in lstItems)                {
                    if (item.TreeItemsCount > 0)                    {
                        node.Nodes.Add(new FolderNode { Text = item.ParentItem, Value = item.ID.ToString(), Expanded = false });
                        AddItems(node.Nodes, item);                    }
                    else
                        node.Nodes.Add(new LeafNode { Text = item.ParentItem, Value = item.ID.ToString() });
                }            
          }            
          return tree.DataBind(nodes);        
}

Finally json result will be returned to the treeview and data will be binded to treeview.
When i select a node in tree, below JS function will be executed.
JavaScript
function SelectNode(args, event) {            
debugger;            
            var tree = $("#treeview").getTreeViewInstance();
            var nodeOptions = tree.getNodeOptions(args);
            var node = "New Accumulator";
            if (nodeOptions.text == "New..") {                            }
                    }
}

Thanks
Kishore Kumar M
Posted
Updated 3-Jun-13 2:54am
v2
Comments
Sunasara Imdadhusen 3-Jun-13 8:29am    
Where is your code?
Sunasara Imdadhusen 3-Jun-13 8:52am    
do not post code in comment box instead of update your question

1 solution

If you are reloading your page, it should then call the controller to reload it. What you are doing, is correct. What is happening to make you think it's not reloading? You could do window.location = 'url' if you want to see what happens then. Have you used chrome to step through your js code to see if the method is called ?
 
Share this answer
 
Comments
KishoreUHG 4-Jun-13 6:17am    
No its reloading but Control is not going to controllor method after setting window.location. We use only ie.
Christian Graus 4-Jun-13 9:07am    
It cannot reload without calling your controller. Do you have a httppost controller and the get is being called or vice versa ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900