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:
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 calledoldNameand one callednewNamewhose values are taken from the variables you defined.You could equivalently have a class represent your parameters:
I prefer the anonymous object approach, your taste may vary.