[英]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.我想在 asp.net mvc 应用程序中使用 Neo4j 官方驱动程序更新我的模型中的值。 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:我从 View 中的此代码中获取
name
值:
@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.您不需要在查询中用引号将参数括起来 - Neo4j 会为您解决这个问题。
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.在您的示例中,
new { oldName, newName }
部分是用于创建具有两个属性的匿名 C# 对象的简写,一个称为oldName
,另一个称为newName
其值取自您定义的变量。
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.我更喜欢匿名对象的方法,你的口味可能会有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.