Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I'm using ASP.NET Web API 2 for the backend and AngularJS for the frontend. I want to display a treeview in AngularJS.

So I need a nested Model in my ASP Web API. The model currently looks like this:

public class Register
{
    public int ID { get; set; }
    public string Name { get; set; }
    public List<Register> Children { get; set; }
}

and the controller like this:

    // GET: api/Registers
    public IQueryable<Register> GetRegister()
    {
        return db.Register;
    }

In my Database I added following examples:

DBExample

The JSON Output shows:

[{"ID":4,"Name":"RootFolder","Children":[{"ID":5,"Name":"Sub1","Children":null}]},{"ID":5,"Name":"Sub1","Children":null}]

The first entry looks fine, but I don't want to output the children itself. It it possible to only output the RootFolder with its Children and not the children itself? Like this:

{"ID":4,"Name":"RootFolder","Children":[{"ID":5,"Name":"Sub1","Children":null}]}

The second problem is, when I want enter a specific Folder ID (For example ID 4(RootFolder)). I only get this as a response:

Specific Entry

Why there are no child elements? The controller is this one:

     // GET: api/Registers/5
    [ResponseType(typeof(Register))]
    public IHttpActionResult GetRegister(int id)
    {
        Register register = db.Register.Find(id);
        if (register == null)
        {
            return NotFound();
        }

        return Ok(register);
    }

Summary:

  • How can I filter the Output to only show the RootFolders with each children but not showing the children objects itself?
  • Why there are no children when I specify a specific ID?
share|improve this question
up vote 0 down vote accepted

I guess you are using entity framework for database operations. For the first point, you need to filter root folder row e.g. for root Register_ID is null

db.Register.Where(x => x.Register_ID == null && x.ID==ID).ToList()

For second point to load root folder's children you can include it's navigation property, for example refer below code.

db.Register.Include(x => x.Children).Where(x => x.Register_ID == null).ToList();
share|improve this answer
    
Thank you very much. I just have some problems I need to solve. Today I don't get any child elements (I didn't changed my code from yesterday). The strange thing is: When Register ID 4 is the child of 5 it doesn't work. When my ID 5 is the child from 4 it's working fine. So child only works when ID is greater then Register_ID. This wasn't a problem yesterday. When I fixed this I will try your suggestion but it looks very fine. Thanks for that! – Tobias Aug 17 at 7:01
    
Got it working fine now. But I can't use the (x => x.Register_ID == null). Register_ID is unknown by the model. I don't know why EF renamed my "Children" Object to "Register_ID" in the Database. When I use x.Children instead of Register_ID I get an Exception, that the object and list can't be compared to null. – Tobias Aug 17 at 8:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.