Click here to Skip to main content
13,147,689 members (28,722 online)
Rate this:
Please Sign up or sign in to vote.
See more:
this is the exception when i try to go to edit form:

system.nullreferenceException {"Object reference not set to an instance of an object."}

my controller:

public ActionResult Edit(int id)
            var ModifierUser = (from u in _db.Utilisateurs
                                where u.IdUser == id
                                select u).First();
            return View(ModifierUser);
        // POST: /Users/Edit/5
        public ActionResult Edit(Utilisateurs ModifierUser)
            var OriginalUser = (from u in _db.Utilisateurs
                                where u.IdUser == ModifierUser.IdUser
                                select u).FirstOrDefault();
            if (!ModelState.IsValid)
                return View(OriginalUser);
            return RedirectToAction("ListUser");

the view:

lt;% foreach (var item in Model) { %>
        <td style="width: 78px; height: 32px;">
        <% using(Html.BeginForm("Edit","Users",new{id=item.IdUser}))
           { %>
           <input type="image" src="../../Image/edit-validated.png"  alt="Edit" 
                 style="height: 18px" />
                 <%} %></td></tr>
Posted 10-Jan-13 23:25pm
Updated 10-Jan-13 23:26pm
Suvabrata Roy 11-Jan-13 5:28am
First check is your Id is returning anything or not
loylmed 11-Jan-13 5:41am
invalidOperationException was unhandled by user code

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hi loymed,

The problem you are having is subtle. The postback Edit overload won't be able to de-serialise the form data back into ModifierUser as binds to items in a list which is a subproperty of Utilisateurs itself. Specifically the new{id=item.IdUser} won't set ModifierUSer.IdUser.
IIRC if you change

this will populate the value, but I could be mis-remembering what I did (normally I'd check but I'm at work). Additionally, if you are updating this value (and it looks like you are) might be, you are going to have problems distinguishing between the old an new user ids.

There are a few other ways to skin this preserving the old/new id (this same RAZOR as you have currently):

public ActionResult Edit(int id, /*... other values you need from the form - names must match*/)
var OriginalUser = (from u in _db.Utilisateurs
                                where u.IdUser == id
                                select u).FirstOrDefault();
/* Rest of code as before*/

or there is also a forms collection you can use which might be neater, depends on what else you are doing.

public ActionResult Edit(FormsCollection formsCollection)
var OriginalUser = (from u in _db.Utilisateurs
                                where u.IdUser == formsCollection["id"]
                                select u).FirstOrDefault();
/* Rest of code as before*/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100