ASP.Net MVC4 で nhibernate を使用してオブジェクトを更新する際に問題があります。このシナリオで更新を行っています。
the application loads an object in the first session
the object is passed up to the UI tier
some modifications are made to the object
the object is passed back down to the business logic tier
the application persists these modifications by calling SaveOrUpdate()
これはすべて、1 つのセッションでのみ発生します。クラス名 NHibernateSessionPerRequest が静的であり、そのコンストラクターは静的 (シングルトン) です。
[HttpPost]
public ActionResult Edit(Menu menu)
{
if (ModelState.IsValid)
{
repository.SaveOrUpdate(menu);
TempData["message"] = string.Format("{0} has been saved", menu.Name);
return RedirectToAction("Index");
}
else
{
// there is something wrong with the data values
return View(menu);
}
}
しかし、メニュー ID はゼロです。元の ID はありません (id は GUID のタイプです)。および SaveOrUpdate() は常に新しいオブジェクトとして扱い、更新せずに保存します。
Edit.cshtml は次のとおりです。
@model MyApp.Domain.Entities.MenuComponent
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<h2>Edit @Model.Name
</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>MenuComponent</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
どうすればオブジェクトを更新できますか?