tags:

views:

175

answers:

2

I'll make it simple, here's the code I've got.

  public ActionResult DeleteNonCIStaffUser(int id)
  {
    return View(_contractsControlRepository.FetchNonCIStaffUserByID(id));
  }

  [HttpPost]
  public ActionResult DeleteNonCIStaffUser(ContractUser contractUser)
  {
    try
    {
      _contractsControlRepository.DeleteNonCIStaffUser(contractUser.User_Key);
      return RedirectToAction("NonCIStaffUsers"); // never reaches this as an exception is thrown!
    }
    catch
    {
      ViewData["ExceptionMessage"] = "Exception caught!";
      return View(contractUser); // I'm expecting this statement to return the ContractUser object instantiated with the same data as the GET action.
    }
  }

The HttpPost contains the ContractUser object. Upon the postback I am left with nothing, the contractUser object no longer exists, which I find rather odd. FetchNonCIStaffByUser(int) brings back a ContractUser object (which works). The delete page shows information from this object, so I know the initial entry works, it's just the postback if there's an error, no information is displayed from the ContractUser object.

Any ideas why this is happening? I'd have expected the contractUser object to be returned to the view.

FIXED

 <%= Html.Hidden("FullName", Model.FullName) %>
 <%= Html.Hidden("User_Key", Model.User_Key) %>

Neither fields were actually being put in form elements in the page so nothing was being posted back. Good to know.

A: 

When redirecting to the action 'NonCIStaffUsers' you need to parse your object. You can either parse it directly like:

//...
return RedirectToAction("NonCIStaffUsers", new { user = contractUser });
//...

public ActionResult NonCiStaffUsers(ContractUser user) { 
  //...
}

or use the TempDataDictionary:

//...
TempData["user"] = contractUser;
return RedirectToUser("NonCIStaffUsers");
//...

public ActionResult NonCiStaffUsers() { 
   ContractUser user = TempData["user"] as ContractUser;
  //...
}

EDIT

Sorry, I missunderstood the question. Are you sure contractUser is being populated properly? Have you added a break point whilst debugging to check it? It may be a mapping issue in your view.

David G
It never reaches that statement, hence why I referred to `contractUser` in `return View(contractUser);`
Kezzer
I've updated my post with a couple of comments in the code, I understand it may have been a little vague. It's handy to know you can pass an object through RedirectToAction though.
Kezzer
Sorry, I missunderstood the question. Are you sure contractUser is being populated properly? Have you added a break point whilst debugging to check it? It may be a mapping issue in your view.
David G
+1  A: 

When the DeleteNonCIStaffUser action is invoked, the contractUser object is populated from the request (provided you use the default model binder), so properties will be automatically bound from information found in the request. This means that you will need to pass all the values of the contractUser when invoking the delete action (you could store them in hidden fields in the page).

Another option would be to pass just the ID to the delete action use FetchNonCIStaffUserByID to populate the model then delete it which of course would make 2 db calls.

Darin Dimitrov
Doh, sorry, this one was correct. See my fix, you were right.
Kezzer