Pulling child node content via ajax (Or some other method)
Hi there.
So I have been developing for Umbraco for a little while now but have never really got too complicated in design. I am about to switch that up but need a little advice.
The new site I am developing with have many dynamic areas where there will be a lookup on child nodes and I need to know the best way to implement this.
Example:
My Tree may look something like this:
Locations > County > Regions > Offices
I would like to be able to select, say, New York (Region) and pull all the Offices that have a property value of NY. I can do this all day using razor, but the caveat is that I would like to do this via AJAX or another method as not to invoke a page refresh as this information will be populated into a modal display or panel of some sort.
How would this be best implemented? Jquery / JSON? Surface controller?
I would do a SurfaceController with a method that returns a JsonResult set, preferally with a viewmodel so that i donĀ“t have to fetch so much data.
The controller:
public class OfficeController : SurfaceController
{
public JsonResult GetChildrenAsJson(int id)
{
var node = new Node(id);
var json = new List<JsonViewModel>();
foreach (var child in node.ChildrenAsList)
{
json.Add(new JsonViewModel()
{
Name = child.Name,
SomeProperty = child.GetProperty("someProperty").Value
});
}
return Json(json, JsonRequestBehavior.AllowGet);
}
}
public class JsonViewModel
{
public string Name { get; set; }
public string SomeProperty { get; set; }
}
Just a quick question. I changed how I pull the data. All locations use the same DocumentType and have a property called "ServiceRegion". This property will contain a space delimited list of states. Example:
"GA AL NY KS VA"
So what I would like to do now is just query the ServiceRegion for the selected state.
Does this look close?
foreach (var child in node.ChildrenAsList)
{
if (child.GetProperty("ServiceRegion").Value.Contains("GA")
{
json.Add(new JsonViewModel()
{
Name = child.Name,
SomeProperty = child.GetProperty("someProperty").Value
});
}
}
Pulling child node content via ajax (Or some other method)
Hi there.
So I have been developing for Umbraco for a little while now but have never really got too complicated in design. I am about to switch that up but need a little advice.
The new site I am developing with have many dynamic areas where there will be a lookup on child nodes and I need to know the best way to implement this.
Example:
My Tree may look something like this:
Locations > County > Regions > Offices
I would like to be able to select, say, New York (Region) and pull all the Offices that have a property value of NY. I can do this all day using razor, but the caveat is that I would like to do this via AJAX or another method as not to invoke a page refresh as this information will be populated into a modal display or panel of some sort.
How would this be best implemented? Jquery / JSON? Surface controller?
Any guidance would be much appreciated.
Thanks in advance.
Phill
I would do a SurfaceController with a method that returns a JsonResult set, preferally with a viewmodel so that i donĀ“t have to fetch so much data.
The controller:
The ajax call:
The Json result:
Hope it helped!
Did this help Philip? Let me know if i can be of anymore help.
Just about to try and implement it now. I will keep you posted!
Many thanks
Just a quick question. I changed how I pull the data. All locations use the same DocumentType and have a property called "ServiceRegion". This property will contain a space delimited list of states. Example:
"GA AL NY KS VA"
So what I would like to do now is just query the ServiceRegion for the selected state.
Does this look close?
Yeah it looks like it could work. Have you tried it out yet?
This is what I ended up doing:
Result is sent to the browser fine. Having a hard time parsing the results in javascript, but I am new to json so I will get there!!
Many thanks for your help!!! I learnt alot
All working now. Thanks again Dennis. Much appreciated. Once site is complete, i'll shot you a url!
Glad to hear that it pointed you in the right direction! Awsome, shoot an URL an ill check it out!! :) Good luck with the rest of the site!!
is working on a reply...