Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq to custom array object
    text
    copied!<p>I am using MVC3/Entity Framework, I need to send a JSON object to be used by a jsTree. The jsTree is calling for the children each time a node is expanded, so each get returns only one set, instead of the whole tree. This is the get function:</p> <pre><code>public ActionResult GetTreeview(string id, string company) { int parentId = Convert.ToInt32(id); var cats = (from x in db.Categories where x.ICG_PARENT_ID == parentId orderby x.ICG_CATEGORY_NAME select new { x.ICG_CATEGORY_ID, x.ICG_PARENT_ID, x.ICG_CATEGORY_NAME }).ToList(); var jsTree = new JsTreeModel[cats.Count]; for (int i = 0; i &lt; cats.Count; i++) { jsTree[i] = new JsTreeModel { data = cats[i].ICG_CATEGORY_NAME, attr = new JsTreeAttribute { id = cats[i].ICG_CATEGORY_ID }, state = "closed" }; } return Json(jsTree, JsonRequestBehavior.AllowGet); } </code></pre> <p>and here is the model:</p> <pre><code>public class JsTreeModel { public string data; public string state; public JsTreeAttribute attr; } public class JsTreeAttribute { public int id; public bool selected; } </code></pre> <p>I am sure a shorter/better way to do this must exist, but I just recently started learning LINQ and lambda expressions (and MVC) and they are not quite intuitive to me yet. Also, I have been using this site as a resource for a long time (it's wonderful) but this is my first question. Is there really no preview button, or am I blind?</p> <p>EDIT: Updated Model and Function to change the type of attr.Id to int. LINQ to Entities didn't like string conversion.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload