Hello,
I am trying to create a TreeView with checkbox support within ASP.NET MVC2. I have successfully created the Tree, and as can be seen from below (code simplified for posting here), each item has a checkbox.
TreeController
TreeHelper.cs
TreeViewItem.cs (Model)
Index.aspx
TreePartial.ascx
TreeViewModel.cs
The problem I am having is with the HttpPost back to the Controller. For some reason (I think it's the way I use recursive partial views to create the tree, but not sure), the view model on the HttpPost back doesn't contain all the items in the TreeView. It only contains the first element (Item One). Any suggestions are appreciated.
Thanks!
I am trying to create a TreeView with checkbox support within ASP.NET MVC2. I have successfully created the Tree, and as can be seen from below (code simplified for posting here), each item has a checkbox.
TreeController
Code:
[HttpGet]
public ActionResult Index()
{
var viewModel = new TreeViewModel
{
rhestrNode = TreeHelper.CreateTree()
};
return View(viewModel);
}
[HttpPost]
public ActionResult Index(TreeViewModel viewModel)
{
if (ModelState.IsValid)
{
return View(viewModel);
}
else
{
return View();
}
}
TreeHelper.cs
Code:
public static List<TreeViewItem> CreateTree()
{
List<TreeViewItem> treeList = new List<TreeViewItem>();
TreeViewItem tvn1 = new TreeViewItem();
tvn1.Checked = false;
tvn1.Value = "Item One";
tvn1.ID = 1;
tvn1.Nodes = new List<TreeViewItem>();
TreeViewItem tvn2 = new TreeViewItem();
tvn2.Checked = true;
tvn2.Value = "Item Two";
tvn2.ID = 2;
tvn2.Nodes = null;
tvn1.Nodes.Add(tvn2);
treeList.Add(tvn1);
return treeList;
}
TreeViewItem.cs (Model)
Code:
public class TreeViewItem
{
public int ID { get; set; }
public string Value { get; set; }
public bool Checked { get; set; }
public IList<TreeViewItem> Nodes { get; set; }
}
Index.aspx
Code:
Inherits="System.Web.Mvc.ViewPage<Prawf.ViewModels.TreeViewModel>"
<% Html.RenderPartial("TreePartial", Model); %>
<input type="submit" value="add" />
TreePartial.ascx
Code:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Prawf.ViewModels.TreeViewModel>" %>
<%@ Import Namespace="Prawf.Models" %>
<ul>
<%
for (int i = 0; i < Model.rhestrNode.Count; i++)
{
Response.Write("<li>");
Response.Write(Html.CheckBoxFor(model => model.rhestrNode[i].Checked));
Response.Write(Html.DisplayTextFor(model => model.rhestrNode[i].Value));
if (Model.rhestrNode[i].Nodes != null && Model.rhestrNode[i].Nodes.Count > 0)
{
this.Model.nodeList = Model.rhestrNode[i].Nodes.ToList();
Html.RenderPartial("TreePartial", this.Model);
}
Response.Write("</li>");
}
%>
</ul>
TreeViewModel.cs
Code:
public class TreeViewModel
{
public List<TreeViewItem> nodeList { get; set; }
}
The problem I am having is with the HttpPost back to the Controller. For some reason (I think it's the way I use recursive partial views to create the tree, but not sure), the view model on the HttpPost back doesn't contain all the items in the TreeView. It only contains the first element (Item One). Any suggestions are appreciated.
Thanks!