-3

I want to pass the result of a cypher query to MVC's view using model. The obtained result is in form of nodes which I get in var result. Now I want to pass this result to a view as model, so that I can print the obtained result in a razor view.

My model class is as:

public class Item {
    public int id {get; set;}
    public string name {get; set;}
    public string type {get; set;}
}

And my controller method is as:

public ActionResult Index()
{
    using(var driver = GraphDatabase.Driver("bolt://localhost:7687","neo4j", "12345")
    {
        using(var session = driver.Session())
        { 
            using(var tx = session.ReadTransaction())
            {
                var result = tx.Run("MATCH (m:Item)  RETURN m")
            }    
        }
    }
    return View();
}

1 Answers1

2

Finally I solved my problem. Thanks to @Chris Skardon. Below is the code that solved my problem.

    public ActionResult Index()
    {
        // Using Neo4j.Driver
        List<Item> items = new List<Item>();
        using (var session = _driver.Session())
        {
            var results = session.ReadTransaction(tx => tx.Run("MATCH (a:Item) RETURN (a)"));

            foreach(IRecord result in results)
            {
                var Node = result["a"].As<INode>();
                var Id = node.Properties["ID"]?.As<long>();
                var Name = node.Properties["Name"]?.As<string>();
                var Type = node.Properties["Type"]?.As<string>();
                items.Add(new Item { id = Id, name = Name, type = Type });
            }
            return View(items.ToList());               
        }

    }