Click here to Skip to main content
Rate this: bad
good
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

        [AcceptVerbs(HttpVerbs.Post)]
        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);
            }
            _db.ApplyCurrentValues(OriginalUser.EntityKey.EntitySetName,ModifierUser);
            _db.SaveChanges();
 
            return RedirectToAction("ListUser");
 
        }
the view:
 
&
lt;% foreach (var item in Model) { %>
    <tr>
      
        <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
loylmed374
Edited 10-Jan-13 23:26pm
v2
Comments
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
good
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
 using(Html.BeginForm("Edit","Users",new{id=item.IdUser}))
to
using(Html.BeginForm("Edit","Users",new{IdUser=item.IdUser}))
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):
 

[AcceptVerbs(HttpVerbs.Post)]
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.
 
[AcceptVerbs(HttpVerbs.Post)]
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*/
}
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.140926.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