-1

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>
}
  • Are there any errors or is this a question of why doesnt my code work? – Simon Price Dec 23 '19 at 12:24
  • This is a question. My code is not working. Why? @Simon – Sami Khan Afridi Dec 24 '19 at 01:48
  • https://stackoverflow.com/help/on-topic - Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. See: How to create a Minimal, Reproducible Example. – Simon Price Dec 24 '19 at 11:12

1 Answers1

0

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.

Pablissimo
  • 2,846
  • 2
  • 19
  • 21
  • I tried this but again I end up with no changes. The query does not update the Name property. I am attaching my model class and view code as well. @Pablissimo – Sami Khan Afridi Dec 24 '19 at 02:07
  • With a breakpoint set in your Edit controller action, do you get sensible values for oldName and newName? Can you use the Neo4j Browser to match a node with the applied oldName? There's not a lot to go on here really. – Pablissimo Dec 24 '19 at 07:17
  • Yes I can update the value using Neo4j browser using the query: Match (a:Category) WHERE a.Name = 'oldName' Set a.Name = 'newName' – Sami Khan Afridi Dec 24 '19 at 08:31
  • How can I get sensible values from the view? @ Pablissimo – Sami Khan Afridi Dec 24 '19 at 08:33