Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET MVC4
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 11-Jan-13 0:25am
Edited 11-Jan-13 0:26am
Suvabrata Roy at 11-Jan-13 5:28am
First check is your Id is returning anything or not
loylmed at 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
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294

Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2014
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