1

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?

6
  • 1
    how is the ID stored in your View to be posted back ? could you post markup for this field ? Commented Oct 18, 2013 at 20:19
  • In view do you have @Model.HiddenFor(x => x.Id) Commented Oct 20, 2013 at 5:43
  • @rippo yes I had @Html.HiddenFor(model => model.ID) but removing this doesnt solved the problem Commented Oct 21, 2013 at 13:05
  • 2
    you should put the @Html.HiddenFor(model => model.ID) back in your code, and remove the private modifier on the setter in public virtual Guid ID { get; private set; } this prevents binding of your ViewModel Commented Oct 21, 2013 at 13:41
  • 1
    @jbl you are a great and clever man. you saved my life. just post your answer to mark it as a lifesaving answer. Commented Oct 21, 2013 at 13:48

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

I have an abstract class named Entity and all of my classes are subclassed from Entity. in Entity I have defined Id property like this: public virtual Guid ID { get; private set; }
while the user's browser is open and browsing my site, the web app only create one session and use this. so there isn't any new session

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.