简体   繁体   中英

How to update a node property with official Neo4j .Net Driver using parameters

I want to update a value in my model using Neo4j official driver for .Net in asp.net mvc app. My code is as:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(string name, Category category)
    {
        try
        {
            var oldName = name.ToString();
            var newName = category.Name.ToString();
            using (var session = _driver.Session())
            {
                session.WriteTransaction(tx =>
                {
                    tx.Run("Match (a:Category) WHERE a.Name = '$oldName' Set a.Name = '$newName'", new { oldName, newName });
                });
            }

            return RedirectToAction(nameof(Index));
        }
        catch
        {
            return View();
        }
    }

But the code results with no changes. Why?

Model class:

 public class Category
{
    public string Name { get; set; }
}

And I get the name value from this code in View:

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { name = item.Name/* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

You don't need to surround the parameters with quotes in your query - Neo4j will sort that out for you.

Try:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(string name, Category category)
{
    try
    {
        var oldName = name.ToString();
        var newName = category.Name.ToString();
        using (var session = _driver.Session())
        {
            session.WriteTransaction(tx =>
            {
                tx.Run("Match (a:Category) WHERE a.Name = $oldName Set a.Name = $newName", new { oldName, newName });
            });
        }

        return RedirectToAction(nameof(Index));
    }
    catch
    {
        return View();
    }
}

In the parameters section, you just need to supply any object whose property names match the names of the parameters in the query. In your example, the new { oldName, newName } section is short-hand for creating an anonymous C# object with two properties, one called oldName and one called newName whose values are taken from the variables you defined.

You could equivalently have a class represent your parameters:

class MyParams {
   public string oldName { get; set; }
   public string newName { get; set; }
}

var p = new MyParams { oldname = name, newName = category.Name };

tx.Run("Match (a:Category) WHERE a.Name = $oldName Set a.Name = $newName", p);

I prefer the anonymous object approach, your taste may vary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM