Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:


C#
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:

&
HTML
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
Updated 10-Jan-13 23:26pm
v2
Comments
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

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
XML
using(Html.BeginForm("Edit","Users",new{id=item.IdUser}))
to
XML
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):


C#
[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.

C#
[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*/
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900