I'm trying to create a self referencing object using linqTOsql mapping. So far, I am definitely in over my head. Here's the code I have:
[Table]
public class Category
{
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
public Int64 catID { get; set; }
public Int64 parentCatID { get; set; }
public string catName { get; set; }
public string catDescription { get; set; }
internal EntityRef<IEnumerable<Category>> _category;
[Association(ThisKey = "parentCatID", Storage = "_category")]
public IEnumerable<Category> category {
get { return _category.Entity; }
set { _category.Entity = value; }
}
}
My fakeRepository is defined like this:
// Fake hardcoded list of categories
private static IQueryable<Category> fakeCategories = new List<Category> {
new Category { catID = 1, parentCatID = 0, catName = "Root", catDescription = "" },
new Category { catID = 2, parentCatID = 1, catName = "Category w/subs", catDescription = "" },
new Category { catID = 3, parentCatID = 1, catName = "Category no subs but now has subs", catDescription = "" },
new Category { catID = 4, parentCatID = 2, catName = "Zub Cat", catDescription = "" },
new Category { catID = 5, parentCatID = 2, catName = "Sub Cat", catDescription = "" },
new Category { catID = 6, parentCatID = 0, catName = "Another Root", catDescription = "" },
new Category { catID = 7, parentCatID = 0, catName = "Ze German Root", catDescription = "" },
new Category { catID = 8, parentCatID = 3, catName = "Brand new cats", catDescription = "" },
new Category { catID = 9, parentCatID = 8, catName = "Brand new cats sub", catDescription = "" },
}.AsQueryable();
I pass Category to the view like this:
public ActionResult CategoryTree()
{
IQueryable<Category> cats = genesisRepository.Category
.Where(x => x.parentCatID == 0)
.OrderBy(x => x.catName);
return View(cats);
}
The problem that I'm running into is that all of this compiles, but I don't get anything beyond the root categories. Model[0].category is returning null.
What is wrong with my self-referencing object?
Edit
I wonder if it's not working because I don't have a real linq-to-sql data context in my fakeRepository. If that's the case, is there a way around that? Can I can get this to work without a connection to a database?
Yeah, you hit the nail on the head. It's not working because you're using a fake repository.
Linq-to-Sql does all the wiring up for you and sets the related collections based on the properties (& their attributes) that you setup in your model.
I don't know how to accomplish this without a connection to the database because internal EntityRef<IEnumerable<Category>> _category; is completely foreign to me - I'm more of a POCO model type of guy.
After a quick google, I found this - How to: Map Database Relationships (LINQ to SQL)
Could you change your model to read:
[Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
public Int64 CatId { get; set; }
[Column]
public Int64 ParentCatId { get; set; }
[Column]
public string CatName { get; set; }
[Column]
public string CatDescription { get; set; }
private EntitySet<Category> _ChildCategories;
[Association(Storage = "_ChildCategories", OtherKey = "ParentCatId")]
public EntitySet<Category> ChildCategories
{
get { return this._ChildCategories; }
set { this._ChildCategories.Assign(value); }
}
private EntityRef<Category> _ParentCategory;
[Association(Storage = "_ParentCategory", ThisKey = "ParentCatId")]
public Category ParentCategory
{
get { return this._ParentCategory.Entity; }
set { this._ParentCategory.Entity = value; }
}
Now because your ChildCategories is of type EntitySet<Category> (which inherits from IList<T>) you should be able to wire fake relationships up yourself.
So you could do something like this:
private static IQueryable<Category> GetFakeCategories()
{
var categories = new List<Category> {
new Category { CatId = 1, ParentCatId = 0, CatName = "Root", CatDescription = "" },
new Category { CatId = 2, ParentCatId = 1, CatName = "Category w/subs", CatDescription = "" },
//Blah
new Category { CatId = 8, ParentCatId = 3, CatName = "Brand new cats", CatDescription = "" },
new Category { CatId = 9, ParentCatId = 8, CatName = "Brand new cats sub", CatDescription = "" }
};
//Loop over the categories to fake the relationships
foreach (var category in categories)
{
category.ChildCategories = new EntitySet<Category>(); //new up the collection
foreach (var subLoopCategory in categories)
{
if (category.ParentCatId == subLoopCategory.CatId)
category.ParentCategory = subLoopCategory;
if (category.Id == subLoopCategory.ParentCatId)
category.ChildCategories.Add(subLoopCategory);
}
}
return categoies.AsQueryable();
}
It works in my head at least... :-)
HTHs,
Charles
EDIT: Re: Comment below about a null reference on _childCategories.
You could change the model to look like:
private EntitySet<Category> _ChildCategories = new EntitySet<Category>();
It is supposed to be null. You are getting all categories where the ParentId = 0 ... and you don't have a child with an Id of 0. So that seems right to me.
It is not showing any subcategories because it has no subcategories to show. Try this:
IQueryable<Category> cats = genesisRepository.Category
.Where(x => x.parentCatID != 0)
.OrderBy(x => x.catName);
The parentCatId needs to point to a valid CatId for it to be a subcategory. This query should get you all the categories that are subcategories.
Related
Ok I have the following extensions I am checking in my code.
FileAttachments.FileAttachmentTypes fileAttachmentType = FileAttachments.FileAttachmentTypes.None;
The strings below in the conts are helled in a constants file
public const string wordExtensions = "docx;docm;doc";
public const string imageExtensions = "gif;tiff;tif;png;eps;raw;bmp;jpeg;jpg";
public const string excelExtensions = "csv;xml;xls;xlsb;xlsm;xlsx";
Which is then parsed down here
I am getting my extension from the File Info class as such
foreach (var fileAttachments in FormFile) {
if (fileAttachments.Length > 0) {
string filePath = Path.Combine(hostingEnvironment.WebRootPath, "Uploads");
var tennantId = GetCurrentTennantId().Result;
var settings = _context.SystemSetup.FirstOrDefault().UploadFolderPath;
string uniqueFilename = Guid.NewGuid().ToString() + "_" + fileAttachments.FileName;
string savedFileName = Path.Combine(filePath, uniqueFilename);
FileInfo infoFile = new FileInfo(savedFileName);
string extension = infoFile.Extension.Replace(".","");
Here I check the file extension Parameter above in the semi colan separated strings
if (extension.Contains(Constants.imageExtensions)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Image;
} else if (extension.Contains("pdf")) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.PDF;
} else if (extension.Contains(Constants.videoFormats)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Video;
} else if (extension.Contains(Constants.excelExtensions)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Excel;
} else if (extension.Contains(Constants.wordExtensions)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Word;
} else if (extension.Contains(Constants.audioExtensions)) {
fileAttachmentType = FileAttachments.FileAttachmentTypes.Voice;
}
}
Types is just an area for categorizing them into tabs for front end display
public enum FileAttachmentTypes {
[Display(Name = "Microsoft Word")]
Word = 1,
[Display(Name = "Microsoft Excel")]
Excel = 2,
[Display(Name = "Image")]
Image = 3,
[Display(Name = "Voice Recordings")]
Voice = 4,
[Display(Name = "PDF")]
PDF = 5,
[Display(Name = "Video")]
Video = 6,
[Display(Name = "Evidence")]
Evidence = 7,
[Display(Name = "None")]
None = 8,
[Display(Name = "Notes")]
NOTES = 9,
[Display(Name = "Shared Case")]
SharedCase = 10,
[Display(Name = "Created Case")]
CreatedCase = 11
}
My Question is why is it not finding it for a word document of extension docx when its clearly there
Using xamarin I would like to add a few pins to map to make a route.
My API for adding pins:
{"Id":1,"X":1.0,"Y":2.0,"RouteId":1,"Route":null}
My API for adding routes:
{"Id":1,"Name":"dd","Description":"fff"}
"RouteId:1" is associated with "Id:1"
I would like to create a route by pressing the button(OnNewRouteClicked)
My code:
public partial class CreatorPage : ContentPage
{
private CustomPin pin;
public CreatorPage()
{
InitializeComponent();
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(53.010281, 18.604922), Distance.FromMiles(1.0)));
}
private void OnClearClicked(object sender, EventArgs e)
{
customMap.Pins.Clear();
customMap.MapElements.Clear();
}
private async void OnMapClicked(object sender, MapClickedEventArgs e)
{
if (String.IsNullOrWhiteSpace(nazwaEntry.Text))
{
await DisplayAlert("Błąd", "Podaj nazwę punktu", "Ok");
return;
}
CustomPin pin = new CustomPin
{
Type = PinType.SavedPin,
Position = new Position(e.Position.Latitude, e.Position.Longitude),
Label = nazwaEntry.Text,
Address = opisEntry.Text,
Name = "Xamarin",
Url = "http://xamarin.com/about/",
Question = zagadkaEntry.Text,
Answer = odpowiedzEntry.Text
};
pin.MarkerClicked += async (s, args) =>
{
args.HideInfoWindow = true;
string pinName = ((CustomPin)s).Label;
// string pytanie = ((CustomPin)s).Question;
string opis = ((CustomPin)s).Address;
// string odpowiedz = ((CustomPin)s).Answer;
await DisplayAlert($"{pinName}", $"{opis}", "Quiz");
// await DisplayAlert("Quiz", $"{pytanie}", "Przejdź do odpowiedzi");
await Navigation.PushAsync(new QuestionPage(new Question()));
};
customMap.CustomPins = new List<CustomPin> { pin };
customMap.Pins.Add(pin);
var json = JsonConvert.SerializeObject(new { X = pin.Position.Latitude, Y = pin.Position.Longitude });
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
var result = await client.PostAsync("URL to points", content);
if (result.StatusCode == HttpStatusCode.Created)
{
await DisplayAlert("Komunikat", "Dodanie puntku przebiegło pomyślnie", "Anuluj");
}
}
private void OnNewRouteClicked(object sender, EventArgs e)
{
}
}
I wrote an example and hope it will help you. In the OnNewRouteClicked method, get all the points which routeId = 1 and draw a polyline with them:
public partial class MainPage : ContentPage
{
List<Points> myPoints { get; set; }
Routes myRoute { get; set; }
public MainPage()
{
InitializeComponent();
//Some data you get from your apis
myRoute = new Routes() { Id = 1 };
myPoints = new List<Points>();
myPoints.Add(new Points() { Id = 1, X = 55.6666, Y = 66.4444, RouteId = 1 }); ;
myPoints.Add(new Points() { Id = 2, X = 52.6666, Y = 68.4444, RouteId = 1 }); ;
myPoints.Add(new Points() { Id = 3, X = 53.6666, Y = 62.4444, RouteId = 1 }); ;
myPoints.Add(new Points() { Id = 1, X = 55.6666, Y = 61.4444, RouteId = 2 }); ;
myPoints.Add(new Points() { Id = 2, X = 51.6666, Y = 65.4444, RouteId = 2 }); ;
myPoints.Add(new Points() { Id = 4, X = 54.6666, Y = 67.4444, RouteId = 1 }); ;
myPoints.Add(new Points() { Id = 5, X = 59.6666, Y = 69.4444, RouteId = 1 }); ;
}
private void OnNewRouteClicked(object sender, EventArgs e)
{
List<Position> positions = new List<Position>();
//Get all the points which RouteId = 1
foreach (var item in myPoints)
{
Points tempPoint = item as Points;
if (tempPoint.RouteId == myRoute.Id)
{
Position tempPosition = new Position(tempPoint.X,tempPoint.Y);
positions.Add(tempPosition);
}
}
//your map
Map map = new Map
{
// ...
};
// instantiate a polyline
Polyline polyline = new Polyline
{
StrokeColor = Color.Blue,
StrokeWidth = 12,
};
//add your positions to polyline.Geopath
foreach (var item in positions)
{
polyline.Geopath.Add(item);
}
// add the polyline to the map's MapElements collection
map.MapElements.Add(polyline);
}
}
public class Routes
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class Points
{
public int Id { get; set; }
public double X { get; set; }
public double Y { get; set; }
public int RouteId { get; set; }
}
I have the following model
class Entry
{
public int Id { get; set; }
public bool IsGroup { get; set; }
public int? ParentId { get; set; }
public List<YearCost> YearCost { get; set; } = new List<YearCost>();
}
class YearCost
{
public int Year { get; set; }
public decimal Cost { get; set; }
}
i have this sample list populated using the models above
static void Main(string[] args)
{
var entries = new List<Entry> {
new Entry
{
Id = 1,
ParentId = null,
IsGroup = true,
},
new Entry
{
Id = 2,
ParentId = 1,
IsGroup = false,
YearCost = new List<YearCost> {
new YearCost { Year = 2019, Cost = 10 },
new YearCost { Year = 2020, Cost = 10 }
}
},
new Entry
{
Id = 3,
ParentId = 1,
IsGroup = true
},
new Entry
{
Id = 4,
ParentId = 3,
IsGroup = true
},
new Entry
{
Id = 5,
ParentId = 4,
IsGroup = false,
YearCost = new List<YearCost> {
new YearCost { Year = 2019, Cost = 15 },
new YearCost { Year = 2020, Cost = 10 }
}
},
new Entry
{
Id = 6,
ParentId = 4,
IsGroup = false,
YearCost = new List<YearCost> {
new YearCost { Year = 2019, Cost = 15 },
new YearCost { Year = 2020, Cost = 10 }
}
},
new Entry
{
Id = 7,
ParentId = 3,
IsGroup = true
},
new Entry
{
Id = 8,
ParentId = 7,
IsGroup = false,
YearCost = new List<YearCost> {
new YearCost { Year = 2019, Cost = 30 },
new YearCost { Year = 2020, Cost = 30 }
}
},
new Entry
{
Id = 9,
ParentId = 7,
IsGroup = false,
YearCost = new List<YearCost> {
new YearCost { Year = 2019, Cost = 20 },
new YearCost { Year = 2020, Cost = 20 }
}
},
new Entry
{
Id = 10,
ParentId = 3,
IsGroup = false,
YearCost = new List<YearCost> {
new YearCost { Year = 2019, Cost = 5 },
new YearCost { Year = 2020, Cost = 5 }
}
},
};
Console.WriteLine(String.Format("{0,10}{1,10}{2,10}{3, 10}{4, 10}", "Id", "Group", "Parent Id", 2019, 2020));
Console.WriteLine(String.Format("{0,10}{1,10}{2,10}{3, 10}{4, 10}", "--", "-----", "---------", "----", "----"));
foreach (var entry in entries.OrderBy(x=>x.ParentId))
{
Console.Write(String.Format("{0,10}{1,10}{2,10}", entry.Id, entry.IsGroup ? "yes" : "no", entry.ParentId?.ToString() ?? "NULL", 2019, 2020));
foreach (var y in entry.YearCost)
Console.Write(String.Format("{0,10}", y.Cost));
Console.WriteLine("\n");
}
}
Rule #1: only entry which is not a group has cost values entered manually by user while the group entry cost is calculated
Rule #2: nesting of groups are allowed.
what i want is to do hierarchical summation for each group as shown in the table below the value inside the square brackets has to be calculated.
Id Group Parent Id 2019 2020
-- ----- --------- ---- ----
1 yes NULL [95] [85]
2 no 1 10 10
3 yes 1 [85] [75]
4 yes 3 [30] [20]
7 yes 3 [50] [50]
10 no 3 5 5
5 no 4 15 10
6 no 4 15 10
8 no 7 30 30
9 no 7 20 20
Thanks in Advance
I've managed finally to have the answer
first you need to group element by parent
var groups = entries.ToLookup(x => x.ParentId).ToDictionary(x => x.Key ?? 0, x
=> x.ToArray().Select(e => e.Id).ToList());
then get all children helpers
private List<int> GetAllChildren(int? parent, Dictionary<int, List<int>> groups)
{
List<int> children = new List<int>();
PopulateChildren(parent, children, groups);
return children;
}
private void PopulateChildren(int? parent, List<int> children, Dictionary<int, List<int>> groups)
{
List<int> myChildren;
if (groups.TryGetValue(parent.Value, out myChildren))
{
children.AddRange(myChildren);
foreach (int child in myChildren)
{
PopulateChildren(child, children, groups);
}
}
}
Then Iterate over the list to populate the totals
foreach (var item in entries)
{
if (item.IsGroup)
{
var children = GetAllChildren(item.Id, groups);
children.ForEach(c => {
var entry = entries.FirstOrDefault(x => x.Id == c);
if(entry != null)
{
if (!item.isGroup)
{
entry.YearCosts?.ForEach(y =>
{
if (item.YearCosts.FirstOrDefault(yx => yx.Year == y.Year) == null)
{
item.YearCosts.Add(new YearCost { Year = y.Year, Cost = 0 });
}
item.YearCosts.FirstOrDefault(yx => yx.Year == y.Year).Cost += y.Cost ?? 0;
item.SubTotal += y.Cost ?? 0;
});
}
}
});
}
}
Hi there i would like to let customers type a category name and get some search results. Currently when you type a category name it says no results.
public ActionResult AdvanceSearch(SearchModel model, CatalogPagingFilteringModel command)
{
//'Continue shopping' URL
_genericAttributeService.SaveAttribute(_workContext.CurrentCustomer,
SystemCustomerAttributeNames.LastContinueShoppingPage,
_webHelper.GetThisPageUrl(false),
_storeContext.CurrentStore.Id);
if (model == null)
model = new SearchModel();
var searchTerms = model.q;
if (searchTerms == null)
searchTerms = "";
searchTerms = searchTerms.Trim();
//sorting
PrepareSortingOptions(model.PagingFilteringContext, command);
//view mode
PrepareViewModes(model.PagingFilteringContext, command);
//page size
PreparePageSizeOptions(model.PagingFilteringContext, command,
_catalogSettings.SearchPageAllowCustomersToSelectPageSize,
_catalogSettings.SearchPagePageSizeOptions,
_catalogSettings.SearchPageProductsPerPage);
string cacheKey = string.Format(ModelCacheEventConsumer.SEARCH_CATEGORIES_MODEL_KEY,
_workContext.WorkingLanguage.Id,
string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()),
_storeContext.CurrentStore.Id);
var categories = _cacheManager.Get(cacheKey, () =>
{
var categoriesModel = new List<SearchModel.CategoryModel>();
//all categories
var allCategories = _categoryService.GetAllCategories(storeId: _storeContext.CurrentStore.Id);
foreach (var c in allCategories)
{
//generate full category name (breadcrumb)
string categoryBreadcrumb = "";
var breadcrumb = c.GetCategoryBreadCrumb(allCategories, _aclService, _storeMappingService);
for (int i = 0; i <= breadcrumb.Count - 1; i++)
{
categoryBreadcrumb += breadcrumb[i].GetLocalized(x => x.Name);
if (i != breadcrumb.Count - 1)
categoryBreadcrumb += " >> ";
}
categoriesModel.Add(new SearchModel.CategoryModel
{
Id = c.Id,
Breadcrumb = categoryBreadcrumb
});
}
return categoriesModel;
});
if (categories.Any())
{
//first empty entry
model.AvailableCategories.Add(new SelectListItem
{
Value = "0",
Text = _localizationService.GetResource("Common.All")
});
//all other categories
foreach (var c in categories)
{
model.AvailableCategories.Add(new SelectListItem
{
Value = c.Id.ToString(),
Text = c.Breadcrumb,
Selected = model.cid == c.Id
});
}
}
IPagedList<Product> products = new PagedList<Product>(new List<Product>(), 0, 1);
// only search if query string search keyword is set (used to avoid searching or displaying search term min length error message on /search page load)
if (Request.Params["q"] != null)
{
if (searchTerms.Length < _catalogSettings.ProductSearchTermMinimumLength)
{
model.Warning = string.Format(_localizationService.GetResource("Search.SearchTermMinimumLengthIsNCharacters"), _catalogSettings.ProductSearchTermMinimumLength);
}
else
{
var categoryIds = new List<int>();
int manufacturerId = 0;
decimal? minPriceConverted = null;
decimal? maxPriceConverted = null;
bool searchInDescriptions = false;
int vendorId = 0;
if (model.adv)
{
//advanced search
var categoryId = model.cid;
if (categoryId > 0)
{
categoryIds.Add(categoryId);
if (model.isc)
{
//include subcategories
categoryIds.AddRange(GetChildCategoryIds(categoryId));
}
}
manufacturerId = model.mid;
//min price
if (!string.IsNullOrEmpty(model.pf))
{
decimal minPrice;
if (decimal.TryParse(model.pf, out minPrice))
minPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(minPrice, _workContext.WorkingCurrency);
}
//max price
if (!string.IsNullOrEmpty(model.pt))
{
decimal maxPrice;
if (decimal.TryParse(model.pt, out maxPrice))
maxPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(maxPrice, _workContext.WorkingCurrency);
}
if (model.asv)
vendorId = model.vid;
searchInDescriptions = model.sid;
}
//var searchInProductTags = false;
var searchInProductTags = searchInDescriptions;
//products
products = _productService.SearchProducts(
categoryIds: categoryIds,
manufacturerId: manufacturerId,
storeId: _storeContext.CurrentStore.Id,
visibleIndividuallyOnly: true,
priceMin: minPriceConverted,
priceMax: maxPriceConverted,
keywords: searchTerms,
//searchDescriptions: searchInDescriptions,
searchProductTags: searchInProductTags,
searchSku: true,
languageId: _workContext.WorkingLanguage.Id,
orderBy: (ProductSortingEnum)command.OrderBy,
pageIndex: command.PageNumber - 1,
pageSize: command.PageSize,
vendorId: vendorId);
model.Products = PrepareProductOverviewModels(products).ToList();
model.NoResults = !model.Products.Any();
//search term statistics
if (!String.IsNullOrEmpty(searchTerms))
{
var searchTerm = _searchTermService.GetSearchTermByKeyword(searchTerms, _storeContext.CurrentStore.Id);
if (searchTerm != null)
{
searchTerm.Count++;
_searchTermService.UpdateSearchTerm(searchTerm);
}
else
{
searchTerm = new SearchTerm
{
Keyword = searchTerms,
StoreId = _storeContext.CurrentStore.Id,
Count = 1
};
_searchTermService.InsertSearchTerm(searchTerm);
}
}
//event
_eventPublisher.Publish(new ProductSearchEvent
{
SearchTerm = searchTerms,
SearchInDescriptions = searchInDescriptions,
CategoryIds = categoryIds,
ManufacturerId = manufacturerId,
WorkingLanguageId = _workContext.WorkingLanguage.Id,
VendorId = vendorId
});
}
}
model.PagingFilteringContext.LoadPagedList(products);
return View(model);
}
A public action method 'AdvanceSearch' was not found on controller 'Nop.Web.Controllers.CatalogController'.
Hi iam try to Get Database data and showed in RadTreeview,
here i got one sample,that sample like default sample,every Root and Child has to be enter manually,
but i want bind from database,values are filled automatically(root/child)
i will show that example code,
List<SiteDataItem> siteData = new List<SiteDataItem>();
siteData.Add(new SiteDataItem(1, 0, "Products"));
siteData.Add(new SiteDataItem(2, 1, "RadControls for ASP.NET Ajax"));
siteData.Add(new SiteDataItem(3, 1, "RadControls for Silverlight"));
siteData.Add(new SiteDataItem(4, 2, "RadGrid"));
siteData.Add(new SiteDataItem(5, 2, "RadScheduler"));
siteData.Add(new SiteDataItem(6, 2, "RadEditor"));
siteData.Add(new SiteDataItem(7, 3, "RadGrid"));
siteData.Add(new SiteDataItem(8, 3, "RadMenu"));
siteData.Add(new SiteDataItem(9, 3, "RadEditor"));
RadTreeView1 .DataTextField = "Text";
RadTreeView1.DataFieldID = "ID";
RadTreeView1.DataFieldParentID = "ParentID";
RadTreeView1.DataSource = siteData;
RadTreeView1.DataBind();
Please help me to do as i want
Please try with the below code snippet.
protected void Page_Load(object sender, System.EventArgs e)
{
List<TestModels> models = new List<TestModels>();
models.Add(new TestModels() { ID = 1, ParentID = null, Text = "a1" });
models.Add(new TestModels() { ID = 2, ParentID = 1, Text = "a2" });
models.Add(new TestModels() { ID = 3, ParentID = 2, Text = "a3" });
models.Add(new TestModels() { ID = 4, ParentID = null, Text = "a4" });
models.Add(new TestModels() { ID = 5, ParentID = 4, Text = "a5" });
RadTreeView1.DataTextField = "Text";
RadTreeView1.DataFieldID = "ID";
RadTreeView1.DataFieldParentID = "ParentID";
RadTreeView1.DataSource = models;
RadTreeView1.DataBind();
}
Class
public class TestModels
{
public int ID { get; set; }
public int? ParentID { get; set; }
public string Text { get; set; }
}
Please Set NULL in ParentID in place-of 0.
Because It consider 0 as value. RadTreeView is not able to found any ID field which have 0 value.
You can try something like this.
DataTable data= GetDataFromDatabase()
RadTreeView1.DataTextField = "TextFieldwhichyouwantshow";
RadTreeView1.DataFieldID = "IDFromdatabase";
RadTreeView1.DataFieldParentID = "ParentIDFromDatabase";
RadTreeView1.DataSource = data;
RadTreeView1.DataBind();