I need to fill tree view with parent child on razor asp.net core but I don't know how to fill tree view parent child
and child can select checkbox and can see related chid for every parent
my code with web form csharp
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillModuleName();
}
}
private void FillModuleName()
{
string sql = "";
clsDB obj = new clsDB(Session["g_ConnectionString"].ToString());
DataSet ds = new DataSet();
DataSet ds1 = new DataSet();
int i = 0;
try
{
sql = "select * from tblWebModule where parentmenucode='00'";
ds = obj.ExecuteQuery(sql);
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
TreeNode TN = new TreeNode();
TN.Text = dr["MenuName"].ToString();
TN.Value = dr["ChildMenuCode"].ToString();
sql = "select * from tblWebModule where parentmenucode='" + dr["ChildMenuCode"].ToString() + "'";
ds1 = obj.ExecuteQuery(sql);
foreach (DataRow rw in ds1.Tables[0].Rows)
{
TreeNode CN = new TreeNode();
CN.Text = rw["MenuName"].ToString();
CN.Value = rw["ChildMenuCode"].ToString();
TN.ChildNodes.Add(CN);
}
tvModules.Nodes.Add(TN);
}
}
}
catch (Exception ex)
{
}
}
on html tree view as below
so please How to organize this code on razor page model as below
page model
public void OnGet()
{
How to load tree view parent and child
}
on treeview.cs.html
{
layout
}
How to link page model with html to load tree view
sample for result expected

4 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Dennis J GordonPosted Jul 28, 2023, 5:53 PM
Create a razor page and add the C# code from the web form to the C# page, add the html from the web form to the razor page.
ahmed salahPosted Jul 5, 2023, 8:25 AM
so please How to convert this function below to return list and if possible can you provide full function after return list of tree node
ahmed salahPosted Jul 4, 2023, 9:32 PM
thanks for support
I try to load tree view on razor asp.net so i make the following steps :
1-create model class to load parent and children as below
2-create list from tree node model for all lists parent with children
3-i will try to fill tree node when page load so i do as following
4- so How to cast and change my code my function below LoadTreeNodes to return tree node so i can use it OnGet function()
so my issue now i cn't cast and convert function above to return tree node
so How to do that
Mohamed Azarudeen ZPosted Jul 4, 2023, 12:34 PM
Hi ahmed
To achieve a similar functionality in ASP.NET Core Razor Pages, you can use the standard HTML
ulandlielements to create a tree-like structure, and then use JavaScript to handle the checkbox selections and visibility of child nodes. Here's a step-by-step guide on how to do this:TreeView.cshtml.cs):Create the Razor Page (
TreeView.cshtml):