ahmed salah

ahmed salah

  • NA
  • 530
  • 148.9k

cannot simplicity convert string to system.collection.generi

Feb 9 2018 2:38 PM

Problem

error cannot simplicity convert string to system.collection.generic.ienumerable on view of index

Details

I need to display every department and his employees following to it in tree view using jtree i downloaded from nugget in mvc 5 visual studio 2015 . as department parent and children employee

Sales(parent)
michel(children)
divid(children)
my database data as following
  1. SELECT        dbo.Departments.DepartmentName, dbo.Employee.EmployeeName, dbo.Employee.DepartmentID  
  2. FROM            dbo.Employee INNER JOIN  
  3.                          dbo.Departments ON dbo.Employee.DepartmentID = dbo.Departments.DepartmentID  
 
 

and treeviewhelper class has following function for children

  1. public TreeView<T> Children(Func<T, IEnumerable<T>> selector)  
  2.        {  
  3.            //  if (selector == null) //throw new ArgumentNullException("selector");    
  4.            _childrenProperty = selector;  
  5.            return this;  
  6.        }  
 

my employee controller as following

  1. public class EmployeeController : Controller  
  2.     {  
  3.         // GET: Employee  
  4.         HRSystem hr = new HRSystem();  
  5.         public ActionResult Index()  
  6.         {  
  7.             IEnumerable<Employee> catgory1 = hr.Employees.Where(x => !x.DepartmentID.HasValue).ToList();  
  8.             return View(catgory1);  
  9.         }  
  10.     }  
 

my error show on view of index as following

 
  1. @model IEnumerable<EmployeeSystem.Employee>  
  2. @using System.Web.UI.WebControls  
  3. @using EmployeeSystem.Models;  
  4.   
  5. <h2>TreeView</h2>  
  6. <link href="~/Content/jsTree/themes/default/style.min.css" rel="stylesheet" />  
  7. <div class="form-body">  
  8.     <div id="jstree">  
  9.         @(Html.TreeView(Model)  
  10.                               .EmptyContent("root")  
  11.                               .Children(m=>m.EmployeeName)  
  12.                               .HtmlAttributes(new { id = "tree" })  
  13.                           .ChildrenHtmlAttributes(new { @class = "subItem" })  
  14.                               .ItemText(m => m.EmployeeName)  
  15.                               .ItemTemplate(  
  16.         @<text>  
  17.             <a href="@item.EmployeeName" desc="@item.EmployeeName">@item.EmployeeName</a>  
  18.         </text>)  
  19.         )  
  20.     </div>  
  21. </div>  
  1. public partial class Employee  
  2.     {  
  3.   
  4.         [Key]  
  5.         public int EmployeeNo { getset; }  
  6.         [StringLength(100)]  
  7.         public string EmployeeName { getset; }  
  8.         public int? DepartmentID { getset; }  
  9.         public virtual Department Department { getset; }  
  10.   
  11.     }  
 

Department model

  1. public partial class Department  
  2.     {  
  3.   
  4.         public Department()  
  5.         {  
  6.             Employees = new HashSet<Employee>();  
  7.         }  
  8.   
  9.         public int DepartmentID { getset; }  
  10.   
  11.         [StringLength(50)]  
  12.         public string DepartmentName { getset; }  
  13.         public virtual ICollection<Employee> Employees { getset; }  
  14.     }  
 

error display on line Children(m=>m.EmployeeName) on view of index

How to solve it