asp.net-mvcasp.net-mvc-4redirecttoaction

MVC Redirect to a different view in another controller


After a user has completed a form in MVC and the post action is underway, I am trying to redirect them back to another view within another model.

Eg. This form is a sub form of a main form and once the user has complete this sub form I want them to go back to the main form.

I thought the following might have done it, but it doesn't recognize the model...

//send back to edit page of the referral
return RedirectToAction("Edit", clientViewRecord.client);

Any suggestions are more that welcome...


Solution

  • You can't do it the way you are doing it. You are trying to pass a complex object in the url, and that just doesn't work. The best way to do this is using route values, but that requires you to build the route values specifically. Because of all this work, and the fact that the route values will be shown on the URL, you probably want this to be as simple a concise as possible. I suggest only passing the ID to the object, which you would then use to look up the object in the target action method.

    For instance:

    return RedirectToAction("Edit", new {id = clientViewRecord.client.ClientId});
    

    The above assumes you at using standard MVC routing that takes an id parameter. and that client is a complex object and not just the id, in which case you'd just use id = clientViewRecord.client