1

For example, can I have an action like,

public class HandleMessageController : Controller
{
    public ActionResult ShowMessage(HandleMessage message)
    {
        return View(message);
    }
}

and in another action of another controller,

RedirectToAction("ShowMessage", "HandleMessage", new HandleMessage(.....));

I tested it and the action ShowMessage always get null message.

Model HandleMessage,

public class HandleMessage
{

    public HandleMessage(string message, string controllerName, string actionName, bool isError)
    {
        ActionName = actionName;
        ControllerName = controllerName;
        Message = message;
        IsError = isError;
    }
.....

Update: The Url after RedirectToAction is

http://localhost:61666/HandleMessage/ShowMessage?ActionName=XXXX&ControllerName=XXXX&Message=The%20message&IsError=False
ca9163d9
  • 27,283
  • 64
  • 210
  • 413

4 Answers4

1

It should work but not the way you trying to use. You can not pass an instance of HandleMessage in a redirect to action or any other "link generating" method. You have to setup a correctroute for it (with correct pattern element name - property mapping) or create a link like ?Message=error&IsError=True (just like the wy in a POST request but now every parameter should be in the query string [action and controller will be come from the routing if you renaming the related properties to Action and Controller]).

Peter Kiss
  • 9,309
  • 2
  • 23
  • 38
  • Should I use string concatenation to generate the Url? I thought Asp.Net MVC will automatically convert the instance to query string. How can I get action and controller from the routing? Thanks. – ca9163d9 Jun 24 '13 at 04:30
  • Hmm, it works with your object instance i've forgot that the route data can be filled with an anonymous object so why can't use a concrete type? Now you have to do to provide a parameterless constructor and it should work now. – Peter Kiss Jun 24 '13 at 05:16
1

Looking at your question, it looks like your model/class doesn't have an empty constructor.

The default .NET model binder needs a default constructor to initialise the class, and it then sets the public properties of the class.

Try adding this code to your class:

public class HandleMessage
{
     public HandleMessage()
     {
     }
}

Also if this still doesn't work, have you tried doing the redirection method this way:

RedirectToAction("ShowMessage", "HandleMessage", new { message =  new HandleMessage(.....) });

EDIT:

Looking into this more, it seems that with more complex objects, the route values aren't the best way to store it.

I had a read of this link and it showed that this is normally handled by using the TempData dictionary. Storing it in here will only last for the request, but allow you to move these complex objects about.

Lastly, I was able to get it working without the use of TempData, by doing exactly as you are, however on the incoming action I didn't specify the binding as HandleMessage, but a signature of:

public ActionResult ShowMessage(string Message, string ControllerName, string ActionName, bool IsError)
{

}
m.t.bennett
  • 1,290
  • 16
  • 34
1

I don't think that you can pass classes to the redirected actions like that. Redirection is done by generating url thus it just can't contain classes.

However here is workaround. You can use TempData.

TempData["some_unique_name"] = new HandleMessage();
RedirectToAction("ShowMessage", "HandleMessage");

And in the redirection action

HandleMessage message = (HandleMessage)TempData["some_unique_name"];
Community
  • 1
  • 1
Satpal
  • 132,252
  • 13
  • 159
  • 168
0

I think the variable name message in the action is messing with the DefaultModelBinder operation. Right now there are two candidate property with name message, one in the action method parameter and one in HandleMessage class. It seems DefaultModelBinder is trying to capture the Message=The%20message value and trying to make it into a HandleMessage instance. You can rename the variable something other than message like msg or so and it should work.

P.S. the HandleMessage class should have a parameter less constructor for DefaultModelBinder to work on.

hope this helps.

shakib
  • 5,449
  • 2
  • 30
  • 39