NealR
NealR

Reputation: 10669

Unable to pass multiple parameters on RedirectToAction

Each of my controller methods need to redirect back to the Index page and send the model object they were posted back to the controller with. However, in one instance I need to send an error message along with the model object. Below is the signature of the Index method:

    public ViewResult Index(ZipCodeIndex search, string unspecifiedAction = "")

Since I only need the error message from one method I made this parameter optional. Here is how I am trying to redirect to the Index from the separate action:

        //the parameter 'updateZip' is a model object of type ZipCodeIndex
        return RedirectToAction("Index", new { search = updateZip, unspecifiedAction = "Error: Action could not be determined. IT has been notified and will respond shortly."} );

All this winds up doing is sending the user back to the original page with the error message "Object reference not set to an instance of an object."

EDIT

After the controller hits the RedirectToAction it simply exits the controller without redirecting to the Index method, and the error "Object refrerence not set to an instance of an object" appears on the view.

Upvotes: 3

Views: 3429

Answers (1)

Satpal
Satpal

Reputation: 133403

You can't pass class object in RedirectToAction, so remove search = updateZip parameter.

If you need it. You can pass it in TempData as an alternative

Modify your Action as

public ViewResult Index(string unspecifiedAction = ""){
      var search = (ZipCodeIndex)TempData["ZipCodeIndexData"];
      //rest of code
}

To redirect

TempData["ZipCodeIndexData"] = updateZip;
return RedirectToAction("Index", new { unspecifiedAction = "Error: Action could not be determined. IT has been notified and will respond shortly."} );

Upvotes: 4

Related Questions