Hashem Aboonajmi
Hashem Aboonajmi

Reputation: 13900

updating object using NHibernate in Asp.net MVC4

I have problem in updating object with nhibernate in ASP.Net MVC4 Im doing the update in this scenario:

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()

all this happen only in one session. I have static a class name NHibernateSessionPerRequest and its constructor is static (singeleton)

 [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);
            }
        }

but menu ID is zero. and doesnt have its original ID (id is type of GUID). and SaveOrUpdate() alway treat it as a new object and save it not update it.

enter image description here

here is 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>

how can I update the object?

Upvotes: 1

Views: 283

Answers (2)

jbl
jbl

Reputation: 15433

From your comments, I see two problems :

  • it seems you have removed @Html.HiddenFor(model => model.ID) from your markup. You should put it back, or your ID won't be stored in the page to be posted back to your Controller.
  • Your ID code is public virtual Guid ID { get; private set; } You should remove the private modifier on the setter. I guess it prevents the ModelBinder to set the property when receiving the posted data

Upvotes: 1

frictionlesspulley
frictionlesspulley

Reputation: 12438

from what you have posted it seems that you are returning the entity to the view and there isn't any concept of view model being used.

Firstly usually entities are defined with private setters which would prevent the id being posted back to the Edit action if you use the entity itself.

Secondly (i am not certain about this)

since you are getting the object in the post back and using a session per request (assumption since it is quite common) nhibernate might treat it as a new entity. I am highly doubtful for the second point but will try re creating this and update the answer

Upvotes: 0

Related Questions